How to delete KVM VM guest using virsh command

I am a new Linux and KVM user. How do I delete a VM guest with virsh command line on Linux operating system? How do I delete a virtual machine called vps42 on Ubuntu or CentOS Linux server?

How to Delete a KVM Guest Using Virsh
Deleting a VM Guest removes its XML configuration by default stored in /etc/ directory. You may also delete a guest’s storage files to completely erase the guest. In this quick tutorial you will learn how to delete a VM guest with virsh on Debian/Ubuntu/RHEL/CentOS Linux servers.

To Delete a KVM Guest Using Virsh:

  1. First, list all running KVM guests using “virsh list” command.
  2. Next, you need to shut down a guest virtual machine using the “virsh shutdown VM” command.
  3. Finally, delete a VM Guest with “virsh undefine VM” command.

Let us see steps in details.

Step 1: List all a VM guests

Type the following command:
# virsh list
# virsh list --all

Sample outputs:

Fig.01: Virsh list a KVM vm

Fig.01: Virsh list a KVM vm

To see information about a domain (VM) called openbsd, enter:
# virsh dumpxml VM_NAME
# virsh dumpxml --domain VM_NAME
# virsh dumpxml --domain openbsd

Note down a guest’s storage files:
# virsh dumpxml --domain openbsd | grep 'source file'
<source file='/nfswheel/kvm/openbsd.qcow2'/>

Step 2: Shutdown the guest

Type the following command to shutdown the vm called openbsd:
# virsh shutdown VM_NAME
# virsh shutdown --domain VM_NAME
# virsh shutdown --domain openbsd

You can force a guest virtual machine to stop with the virsh destroy command:
# virsh destroy VM_NAME
# virsh destroy --domain VM_NAME
# virsh destroy --domain openbsd

Step 3: Deleting a virtual machine

To delete a VM Guest with virsh run
# virsh undefine VM_NAME
# virsh undefine --domain VM_NAME
# virsh undefine --domain openbsd

Sample outputs:

Domain openbsd has been undefined

There is no option to automatically delete the attached storage file called /nfswheel/kvm/openbsd.qcow2. To delete it, enter:
# rm -rf /nfswheel/kvm/openbsd.qcow2

A note about error: “cannot delete inactive domain with snapshots”

You cannot delete a VM with snapshots. For example:
# virsh undefine --domain openbsd
Sample outputs:

error: Failed to undefine domain openbsd
error: Requested operation is not valid: cannot delete inactive domain with 2 snapshots

To list snapshots, enter:
# virsh snapshot-list --domain VM_NAME
# virsh snapshot-list --domain openbsd

Sample outputs:

 Name                 Creation Time             State
 3sep2016             2016-09-02 13:38:18 -0500 shutoff
 3sep2016u1           2016-09-02 15:04:50 -0500 shutoff

The syntax is as follows to delete snapshot:
# virsh snapshot-delete --domain VM_NAME --snapshotname SNAPSHOT_NAME
To delete both snapshots, enter:
# virsh snapshot-delete --domain openbsd --snapshotname 3sep2016
Domain snapshot 3sep2016 deleted

# virsh snapshot-delete --domain openbsd --snapshotname 3sep2016u1
Domain snapshot 3sep2016u1 deleted

Verify it:
# virsh snapshot-list --domain openbsd
Name Creation Time State

Now delete the vm:
# virsh undefine --domain openbsd
Domain openbsd has been undefined
# rm -f /nfswheel/kvm/openbsd.qcow2

Remove associated VM storage volumes and snapshots while undefineing a domain/VM

The recent version of virsh command includes support to delete associated storage volumes using the following syntax:
# virsh undefine --domain {VM_NAME_HERE} --storage {source_path}
Use the following command to find out storage ID and source path:
# virsh domblklist --domain {VM_NAME_HERE}
## List VM snapshots ##
# virsh snapshot-list --domain {VM_NAME_HERE}

There is even an option to eliminate all associated storage VM cloud storage volumes which must be used with caution:
# virsh undefine --domain {VM_NAME_HERE} --remove-all-storage
To zap snapshots associated with volume (VM storage) use the following syntax along with the option:
# virsh undefine --domain {VM_NAME_HERE} --delete-snapshots --remove-all-storage
We can also wipe out data on the removed volumes for security reasons so no one can recover data later on:
# virsh undefine --domain {VM_NAME_HERE} --storage {source_path} --wipe-storage

Removing and deleting a virtual machine on Linux with all storage

In short use the following simple syntax for deleting a VM named mysql-server:
# virsh undefine --domain mysql-server --remove-all-storage
# virsh undefine --domain mysql-server --remove-all-storage --delete-snapshots
Removing AND Deleting a Virtual Maching using Virsh on Linux


In this quick tutorial, you learned about deleting a KVM virtual machine (VM) along with storage and snapshots using various command line options. See virsh man page by typing the following man command or online here:
man virsh

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 1 comment so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
1 comment… add one
  • Eden Caldas Feb 9, 2017 @ 7:26

    As of today, the undefine command has a –delete-snapshots and –remove-all-storage which are self-explanatory.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum