How to delete KVM Virtual machine using virsh

Deleting a KVM virtual machine using “virsh” is a multi-step process. You need to stop the virtual machine, find the storage device used by them VM, and remove the storage devices. Then undefine the virtual machine.

Here are the commands used to delete a KVM VM with the name win10.

First shutdown the VM

virsh shutdown win10

If it did not stop, you can force stop with the command

virsh destroy win10

Find information about the VM with the command “virsh dumpxml –domain VM_NAME”

root@mail:~# virsh dumpxml --domain win10

  win10
  e40399f7-9936-41ce-9a70-0251cb948cae
  
    
      
    
  
  16777216
  16777216
  4
  
    hvm
    
  
  
    
    
    
      
      
      
    
    
  
  
  
    
    
    
    
  
  destroy
  restart
  destroy
  
    
    
  
  
    /usr/bin/qemu-system-x86_64
    
      
      
      
      

In the above result, you can see we are using the following storage device for this VM

    
      
      
      
      

This VM uses the storage device /dev/vg1/win10, we need to remove it.

lvremove /dev/vg1/win10

The VM also uses ISO file /var/lib/libvirt/images/Win10_21H2_English_x64.iso, if you don’t need it, you can delete it.

rm -f /var/lib/libvirt/images/Win10_21H2_English_x64.iso

To delete the VM, you can use

virsh undefine win10

Example

root@mail:~# virsh undefine win10
Domain win10 has been undefined

root@mail:~# 

Back to virsh

Need help with Linux Server or WordPress? We can help!

Leave a Reply

Your email address will not be published. Required fields are marked *