Remove Virtual Machine in KVM

linux kvm unixtutorial

I’ve been tidying up some of my dedicated servers and needed to remove some of the VMs under KVM setup. This post shows you how to use virsh command to do just that.

List virtual machines using virsh

As you can see, there are quite a few VMs not running and possibly pending decommission:

root@s2:/ # virsh list --all

Id Name State
----------------------------------------------------
1 m running
2 dbm1 running
3 v15 running
- centos7 shut off
- elk shut off
- infra shut off
- jira shut off
- v10.ts.im shut off
- v9.ts.im shut off

List VM storage using virsh

centos7 VM was definitely there for some quick test, so should be safe to remove.

Let’s confirm the virtual disk files it has:

root@s2:/ # virsh dumpxml --domain centos7 | grep source
<source file='/var/lib/libvirt/images/rhel7.0-3.qcow2'/>
<source bridge='vbr1'/>
<source bridge='vbr0'/>

This is a large enough file with virtual disk:

root@s2:/var/lib/docker/containers # ls -lad /var/lib/libvirt/images/rhel7.0-3.qcow2
-rw------- 1 root root 17182752768 Apr 11 2018 /var/lib/libvirt/images/rhel7.0-3.qcow2
root@s2:/var/lib/docker/containers # du -sh /var/lib/libvirt/images/rhel7.0-3.qcow2
17G /var/lib/libvirt/images/rhel7.0-3.qcow2

Remove KVM virtual machine with storage files

Time to remove our virtual machine and its virtual storage:

root@s2:/var/lib/docker/containers # virsh undefine centos7 --remove-all-storage
Domain centos7 has been undefined
Volume 'vda'(/var/lib/libvirt/images/rhel7.0-3.qcow2) removed.

That’s it for today!

See Also