How to Completely Remove a KVM Guest VM Created with virt-install: Destroy, Undefine and Delete Storage


1 views

When you create a KVM guest using virt-install, several components are generated:

  • The domain definition (stored in libvirt)
  • The virtual machine instance (running state)
  • The storage volume (in your case, an LVM volume)

Here's the complete procedure to cleanly remove all traces of your KVM guest:

# First, check the current state of your VM
virsh list --all

# If running, destroy the active domain
virsh destroy virt1.example.com

# Remove the domain definition
virsh undefine virt1.example.com

# Delete associated storage
virsh vol-delete --pool vg0 virt1.example.com.img

# Verify removal
virsh list --all
virsh vol-list --pool vg0

For newer libvirt versions, you can combine operations:

# Remove everything in one command (libvirt ≥ 1.1.1)
virsh undefine --domain virt1.example.com --remove-all-storage

If you encounter "cannot delete active domain" error:

# Force shutdown if destroy fails
virsh shutdown --domain virt1.example.com --mode acpi
# Wait for shutdown or force it after timeout
virsh destroy virt1.example.com

To completely clean network interfaces:

# List network interfaces
virsh domiflist virt1.example.com
# Remove leftover network configurations

After removal, check these locations:

  • /etc/libvirt/qemu/ (for any leftover XML files)
  • virsh net-dhcp-leases default (for DHCP leases)
  • Your storage pool (verify space was reclaimed)

When you create a KVM guest using virt-install, several components are generated:

1. Domain configuration (XML definition)
2. Virtual disk storage
3. Runtime state information

First verify the VM's status using:

virsh list --all

Sample output:

 Id Name                 State
----------------------------------
  3 virt1.example.com    running

Forcibly terminate the VM if it's running:

virsh destroy virt1.example.com

This is equivalent to pulling the power plug on a physical machine. For graceful shutdown (if guest supports it):

virsh shutdown virt1.example.com

Remove the VM configuration from libvirt:

virsh undefine virt1.example.com

Additional options you might need:

--remove-all-storage   # Remove associated storage volumes
--snapshots-metadata   # Remove snapshot metadata
--nvram               # Remove nvram file (for UEFI guests)

Identify and remove the storage volume:

virsh vol-list vg0
virsh vol-delete --pool vg0 virt1.example.com.img

For newer libvirt versions (1.2.8+), you can combine steps:

virsh undefine --domain virt1.example.com --remove-all-storage

Confirm complete removal:

virsh list --all
virsh vol-list vg0
ls /etc/libvirt/qemu/ | grep virt1.example.com

If you encounter errors:

Error 1: "Requested operation is not valid: cannot delete active domain"
Solution: Ensure VM is stopped first (virsh destroy)

Error 2: "Storage volume not found"
Solution: Verify pool name with 'virsh pool-list'

Error 3: "Permission denied"
Solution: Run commands as root or use sudo

Here's the full process from creation to deletion:

# Create VM
virt-install --name testvm --ram 2048 --disk pool=default,size=20 --vcpus 2 \
--os-type linux --os-variant ubuntu20.04 --graphics none --console pty

# Verify creation
virsh list --all
virsh vol-list default

# Destroy and remove
virsh destroy testvm
virsh undefine testvm --remove-all-storage

For thorough cleanup:

# Remove DHCP lease
virsh net-dumpxml default | grep "testvm"
# Then edit network config and restart libvirt

# Remove cached OS install files
rm -rf /var/lib/libvirt/boot/testvm*

# Check for leftover processes
ps aux | grep qemu | grep testvm