How-To: Using VBoxManage to Delete a Virtual Machine from VirtualBox

deleteOne common problem I see from people learning to manage Virtual Machines within VirtualBox using the VBoxManager command line is the error:

Cannot unregister the machine <Virtual-Machine-Name>  because it has 1 hard disks attached

Before a VM can be unregistered or deleted, all disks that have been “attached” need to first be disassociated from the VM. For this example, I’m using a virtual machine that I have set up and named “VNAS” that was used to test an OpenFiler NAS installation (which, by the way, worked great). Now that testing is complete, I am ready to remove it.

The removal for a standard VM install can be completed in a short list of steps.

Step one: Get the name or UUID and details of your VM

VBoxManage list vms

VirtualBox Command Line Management Interface Version 3.0.8
(C) 2005-2009 Sun Microsystems, Inc.
All rights reserved.

“VNAS” {8a48759d-4e6d-473f-ab0f-8ff59aa727eb}

That command will return the names of all registered VMs and their UUID. Make sure you get the correct name or UUID for the VM you will be modifying.

Step two: Get the disc information for your VM

There are two methods you can use to the get the information for each virtual disk registered to a virtual machine. The first is to list the VM info for a specified VM which includes the Virtual Disks that are in use by it.

VBoxManage showvminfo <Virtual Machine Name or UUID>

VBoxManage showvminfo VNAS
VirtualBox Command Line Management Interface Version 3.0.8
(C) 2005-2009 Sun Microsystems, Inc.
All rights reserved.

Name: VNAS
Guest OS: Other/Unknown
UUID: 8a48759d-4e6d-473f-ab0f-8ff59aa727eb
Config file: /secondary/.Virtual/machines/VNAS/VNAS.xml
Memory size: 512MB
VRAM size: 8MB
Number of CPUs: 1
Boot menu mode: message and menu
Boot Device (1): Floppy
Boot Device (2): DVD
Boot Device (3): HardDisk
Boot Device (4): Not Assigned
ACPI: on
IOAPIC: off
PAE: on

Primary master: /secondary/.Virtual/vdisks/OFInstallDisk (UUID: xxxxxxx)
Primary slave: /secondary/.Virtual/vdisks/VNASStore1 (UUID: xxxxxxx)
Secondary slave: /secondary/.Virtual/vdisks/VNASStore2 (UUID: xxxxxxx)

In this example you can see I have 3 disks registered with the vm.

  • Primary Master (hda)
  • Primary Slave (hdb)
  • Secondary Slave (hdd)

NOTE: You might be asking, Why does the sequence jump from hdb to hdd? What happened to hdc? The answer is, hdc is reserved for CD/DVD mounting, so you can’t add a virtual disk there.

The second, alternative method is to use the the VBoxManage command “list” with the “hdds” option to list all the Virtual Disks in the system. This will display a list of All the disks you have created and registered for ALL VMs. Information for each Virtual Disk and the VM it is registered to is displayed. Here you can see the three disks registered with my VNAS VM indicated by the “Usage” lines.

VBoxManage list hdds

UUID:         eebbb4c7-c539-4da4-b8ab-e5777b32520b
Format:       VDI
Location:     /secondary/.Virtual/vdisks/VNASStore1
Accessible:   yes
Usage:        VNAS (UUID: 8a48759d-4e6d-473f-ab0f-8ff59aa727eb)

UUID:         9f238d31-f794-420f-83a7-ccf811d52244
Format:       VDI
Location:     /secondary/.Virtual/vdisks/VNASStore2
Accessible:   yes
Usage:        VNAS (UUID: 8a48759d-4e6d-473f-ab0f-8ff59aa727eb)

UUID:         2ce976c9-0a37-49c1-8d34-3962c563a256
Format:       VDI
Location:     /secondary/.Virtual/vdisks/OFInstallDisk
Accessible:   yes
Usage:        VNAS (UUID: 8a48759d-4e6d-473f-ab0f-8ff59aa727eb)

In my opinion, method one the easiest quickest way to see what Virtual disks are assigned to a specific VM.

Step Three: Disassociate the Virtual Disk[s]

VBoxManage modifyvm <Virtual Machine Name or UUID> —hda none

VBoxManage modifyvm VNAS –hda none

If you have more than one disk registered with a vm as I do here, you’ll also need to disassociate those as well.

VBoxManage modifyvm VNAS —hdb none

VBoxManage modifyvm VNAS —hdd none

Step Four: Unregister and delete the VM

VBoxManage unregistervm <Virtual Machine Name or UUID> –delete

VBoxManage unregistervm VNAS –delete

Step Five: Confirm

VBoxManage list vms

VirtualBox Command Line Management Interface Version 3.0.8
(C) 2005-2009 Sun Microsystems, Inc.
All rights reserved.

[NOTE: there are no VMs listed]

This is the same command that was executed in step one. After running this command again you should no longer see your Virtual Machine listed.

Note: This process will only remove your VM from VirtualBox. It will not remove/delete the actual virtual disk file (.vdi). You can delete that manually, or save it to use with another virtual machine instance.