3

I have multiple inactive domains in libvirt and I want to delete them all:

# virsh list --inactive
 Id Name                 State
----------------------------------
  - instance-0000000c    shut off
  - instance-0000000f    shut off
  - instance-00000010    shut off

Is there a simple way to tell libvirt to delete all inactive domains, or do I have to manually do:

# virsh undefine instance-0000000c
# virsh undefine ...
Lorin Hochstein
  • 4,868
  • 15
  • 54
  • 72

2 Answers2

4

There is a much cleaner way to do it:

virsh list --inactive --name | xargs -r -n 1 virsh undefine

Be careful, it will removall all. Also it will not fail when there is nothing to clean up.

sorin
  • 7,668
  • 24
  • 75
  • 100
1

Something like this should work:

$ virsh list --inactive | grep instance | cut -d " " -f 4 | xargs -n 1 virsh undefine

If you want to review that list before killing them:

$ virsh list --inactive | grep instance | cut -d " " -f 4 > inactive_domains
$ vi inactive_domains

Then if everything looks good:

$ cat inactive_domains | xargs -n 1 virsh undefine
EEAA
  • 108,414
  • 18
  • 172
  • 242