Shell scripting List and delete VMs - ganeshahv/Contrail_SRE GitHub Wiki

  1. Retrieve VMs using virsh list --all
  2. Retrieve VMs info without the header using virsh list --all | tail -n +3
  3. Retrieve only the VMs names using virsh list --all | tail -n +3 | awk '{print $2}'
  4. Delete the VMs using the following command:
for vmname in ` virsh list --all | tail -n +3 | awk '{print $2}'` ; do
    virsh destroy ${vmname}
    virsh undefine ${vmname}
    virsh vol-delete /home/images/${vmname}.img
done

Credits: [email protected]