Connect with us

Hi, what are you looking for?

Latest

How to delete a VM KVM guest using a virsh command

How to delete a VM KVM guest using a virsh command

How to delete a VM KVM guest using a 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 VM KVM guest using a virsh command
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:
How to delete a VM KVM guest using a virsh command 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’

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
How to delete a VM KVM guest using a virsh command

Conclusion

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

ADVERTISEMENTS

Posted by: Vivek Gite

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.virsh undefine remove-all-storage,virsh start vm,virsh commands,uninstall kvm centos 7,virsh delete snapshot,virsh remove kvm,virsh shutdown all vm's,virsh list delete vm

You May Also Like

Hosting

The latest round of MITRE ATT&CK evaluations proved yet again that Microsoft customers can trust they are fully protected even in the face of...

Hosting

On Thursday, April 23rd, 2020, Canonical Ltd, the makers of Ubuntu Linux distribution officially released the long-awaited Ubuntu 20.04 version code-named “Focal Fossa”, it...

Hosting

As the robot process automation (RPA) market becomes more and more dynamic, more and more companies are trying to integrate RPA into their business...

Latest

Virtual Machine Manager is one of the best hypervisors available for the Linux desktop. This is well-designed and well-functioning QEMU/KVM virtualization software that takes...