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?

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
Sample outputs:

Fig.01: Virsh list a KVM vm

To see info about domain 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

Posted by: SXI ADMIN

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.


How to KVM, QEMU start or stop virtual machine from command line (CLI)

KVM or Kernel Based Virtual Machine is a popular virtualization technology. It allows you to run virtual guest machines over a host machine. To start...

How to Docker backup Saving and restoring your volumes

Running a Docker volume backup First, we spin up a temporary container, and we mount the backup folder and the target Docker volume to this container....

How to Start and Enable Firewalld on CentOS 7

In this article, we discuss how to start and enable firewalld. It is highly recommended that you have a firewall protecting your server.Pre-Flight CheckThese...