IoDeleteDevice

Deletes a device object.

Syntax

VOID IoDeleteDevice(
  _In_ PDEVICE_OBJECT  DeviceObject
);

Parameters

Parameter Type Description
DeviceObject _In_ PDEVICE_OBJECT A pointer to the device object to be deleted. This pointer must have been obtained from a previous call to IoCreateDevice or a similar routine.

Remarks

A driver calls IoDeleteDevice to delete a device object that it previously created using IoCreateDevice or IoCreateSymbolicLink. This routine can only be called by the driver that owns the device object. Typically, a driver calls IoDeleteDevice during its Unload routine, or when it is disabling a particular device.

Before calling IoDeleteDevice, the driver must ensure that the device object is no longer referenced by any outstanding I/O Request Packets (IRPs) and that no other driver components or user-mode applications are holding a handle to the device. The driver should also have removed any symbolic links associated with the device object.

Note: Deleting a device object is a critical operation. Incorrect usage can lead to system instability or crashes. Ensure all cleanup operations are completed before calling this function.

Example

The following code snippet illustrates how a driver might delete a device object in its Unload routine:

VOID MyDriverUnload(
  _In_ PDRIVER_OBJECT DriverObject
)
{
    PDEVICE_OBJECT deviceObject = DriverObject->DeviceObject;

    // Assume deviceObject points to the device object created by this driver.

    if (deviceObject != NULL) {
        // Perform any necessary cleanup for the device
        // ...

        // Remove any symbolic links
        // IoDeleteSymbolicLink(&SymbolicLinkName);

        // Delete the device object
        IoDeleteDevice(deviceObject);
        DriverObject->DeviceObject = NULL; // Important to nullify the pointer
    }

    // Additional cleanup for the driver object itself
    // ...
}