---
title: "detach(device:completionHandler:)"
framework: virtualization
role: symbol
role_heading: Instance Method
path: "virtualization/vzusbcontroller/detach(device:completionhandler:)"
---

# detach(device:completionHandler:)

Detaches a USB device from the controller.

## Declaration

```swift
func detach(device: any VZUSBDevice, completionHandler: @escaping ((any Error)?) -> Void)
```

```swift
func detach(device: any VZUSBDevice) async throws
```

## Parameters

- `device`: The USB device to detach.
- `completionHandler`: A block the framework calls after the device detaches, or on an error. The error parameter that passes to the block is nil if detaching the device is successful. The framework calls the block on a VM’s queue.

## Discussion

Discussion If the device successfully detaches from the controller, it disappears from the usbDevices property, the framework sets its usbController property to nil, and the completion handler returns nil. If the device doesn’t have an attachment to the controller at the time of calling the detach method, the process fails with VZError.Code.deviceNotFound. You need to call this method on the virtual machine’s queue.

## See Also

### Related Documentation

- [VZUSBDevice](virtualization/vzusbdevice.md)

### Attaching and detaching devices

- [attach(device:completionHandler:)](virtualization/vzusbcontroller/attach(device:completionhandler:).md)
