public Task removeDevices ( List < VirtualDevice > deviceList , boolean destroyDeviceBacking ) throws InvalidName , VmConfigFault , DuplicateName , TaskInProgress , FileFault , InvalidState , ConcurrentAccess , InvalidDatastore , InsufficientResourcesFault , RuntimeFault , RemoteException { List < VirtualDeviceConfigSpec > configSpecList = new ArrayList < VirtualDeviceConfigSpec > ( ) ; boolean allDevicesSupportHotRemoval = allSupportHotRemoval ( deviceList ) ; VirtualMachinePowerState powerState = vm . getRuntime ( ) . getPowerState ( ) ; if ( ! allDevicesSupportHotRemoval && powerState != VirtualMachinePowerState . poweredOff ) { throw new RuntimeException ( "Invalid power state: power off the VM first." ) ; } for ( VirtualDevice device : deviceList ) { try { // Empty SCSI controller removal // If the device's SCSI controller will get free after device removal // then also remove the controller. But only if the VM is powered off. if ( device instanceof VirtualDisk && powerState == VirtualMachinePowerState . poweredOff ) { List < VirtualSCSIController > contollerList = getVirtualDevicesOfType ( VirtualSCSIController . class ) ; for ( VirtualSCSIController controller : contollerList ) { if ( controller . key == device . controllerKey ) { if ( controller . device . length == 1 && controller . device [ 0 ] == device . key ) { VirtualDeviceConfigSpec controllerSpec = new VirtualDeviceConfigSpec ( ) ; controllerSpec . operation = VirtualDeviceConfigSpecOperation . remove ; controllerSpec . device = controller ; configSpecList . add ( controllerSpec ) ; } break ; } } } //region Usb controller removal. // If the device's SCSI controller will get free after device removal, also remove the controller. if ( device instanceof VirtualUSB ) { //TODO remove duplication of this and above section of code List < VirtualUSBController > contollerList = getVirtualDevicesOfType ( VirtualUSBController . class ) ; for ( VirtualUSBController controller : contollerList ) { if ( controller . key == device . controllerKey ) { if ( controller . device . length == 1 && controller . device [ 0 ] == device . key ) { VirtualDeviceConfigSpec controllerSpec = new VirtualDeviceConfigSpec ( ) ; controllerSpec . operation = VirtualDeviceConfigSpecOperation . remove ; controllerSpec . device = controller ; configSpecList . add ( controllerSpec ) ; } break ; } } // remove the Usb controller when we want to remove the usb device. continue ; } VirtualDeviceConfigSpec deviceSpec = new VirtualDeviceConfigSpec ( ) ; deviceSpec . operation = VirtualDeviceConfigSpecOperation . remove ; deviceSpec . device = device ; if ( destroyDeviceBacking ) { deviceSpec . fileOperation = VirtualDeviceConfigSpecFileOperation . destroy ; } configSpecList . add ( deviceSpec ) ; } catch ( Exception ex ) { throw new RuntimeException ( ex ) ; } } if ( configSpecList . size ( ) > 0 ) { VirtualMachineConfigSpec config = new VirtualMachineConfigSpec ( ) ; config . deviceChange = new VirtualDeviceConfigSpec [ configSpecList . size ( ) ] ; for ( int i = 0 ; i < configSpecList . size ( ) ; i ++ ) { config . deviceChange [ i ] = configSpecList . get ( i ) ; } return vm . reconfigVM_Task ( config ) ; } return null ; }
Remove all devices as listed in the deviceList . These devices can be different types . Make sure the VM is powered off before calling this method . If destroyDeviceBacking is true it deletes backings for example files in datastore . BE CAREFUL!