26

I created a KVM guest from the command line with virt-install where I used the command line options --disk pool=vg0,size=20 and --name virt1.example.com.

How can I delete this KVM guest from the command line? I don't need it anymore.

Just for the record the whole command line was

virt-install --debug --hvm --vnc --name virt1.example.com --os-type=linux --os-variant=rhel6 --pxe --network network=default,model=e1000,mac=02:54:00:13:be:e4 --disk pool=vg0,size=20 --ram 1024 --vcpus=1 

and here is some extra information

[root@server ~]# virsh vol-list vg0
Name                 Path                                    
-----------------------------------------
lv0                  /dev/vg0/lv0                            
lv_swap              /dev/vg0/lv_swap                        
virt1.example.com.img /dev/vg0/virt1.example.com.img  

[root@server ~]# virsh list
 Id Name                 State
----------------------------------
  3 virt1.example.com running

Update

Running these three commands seems to remove the installed KVM

virsh destroy virt1.example.com
virsh undefine virt1.example.com
virsh vol-delete --pool vg0 virt1.example.com.img

For more details see the terminal session:

[root@server ~]# virsh list --all
 Id Name                 State
----------------------------------
 3 virt1.example.com running

[root@server ~]# virsh undefine virt1.example.com
error: Failed to undefine domain virt1.example.com
error: Requested operation is not valid: cannot delete active domain

[root@server ~]# virsh destroy virt1.example.com
Domain virt1.example.com destroyed

[root@server ~]# virsh list --all
 Id Name                 State
----------------------------------
  - virt1.example.com shut off

[root@server ~]# virsh undefine virt1.example.com
Domain virt1.example.com has been undefined

[root@server ~]# virsh list --all
 Id Name                 State
----------------------------------

[root@server ~]# virsh vol-list --pool vg0
Name                 Path                                    
-----------------------------------------
lv0                  /dev/vg0/lv0                            
lv_swap              /dev/vg0/lv_swap                        
virt1.example.com.img /dev/vg0/virt1.example.com.img  

[root@server ~]# virsh vol-delete --pool vg0 virt1.example.com.img
Vol virt1.example.com.img deleted

[root@server ~]# virsh vol-list --pool vg0
Name                 Path                                    
-----------------------------------------
lv0                  /dev/vg0/lv0                            
lv_swap              /dev/vg0/lv_swap                        

[root@server ~]# cat /etc/issue
CentOS Linux release 6.0 (Final)
Kernel \r on an \m

[root@server ~]# virsh --version
0.8.1
Erik Sjölund
  • 1,965
  • 5
  • 21
  • 26
  • 1
    For others looking at this answer, if your error is at the virsh undefine virt1.example.com step (with an error like 'Refusing to undefine while domain managed save image exists'). Then you may need an additional command like: virsh managedsave-remove virt1.example.com. – nmtoken Aug 05 '14 at 12:09

1 Answers1

30

According to the manual of virsh this is:

virsh destroy _domain-id_
virsh undefine _domain-id_
virsh vol-delete --pool vg0 _domain-id_.img

And it further says: "Undefine (destroy) the configuration for an inactive domain. Since the domain is not running the domain name or UUID must be used as the domain-id."

mailq
  • 16,882
  • 2
  • 36
  • 66
  • Thanks @mailq I think I managed to delete the KVM. Maybe you could update your answer accordingly? – Erik Sjölund Aug 11 '11 at 12:35
  • Now it reflects the solution – mailq Aug 11 '11 at 12:49
  • 5
    For those who are curious, this is what those commands do: `virsh destroy` does a forced shutdown of the vm, `virsh undefine` removes the configuration from KVM, and `virsh vol-delete` removes the actual disk image. If you need to get the `_domain-id_` type `virsh list --all` to get a list of your VMs. – Rick Chatham Sep 30 '15 at 23:24
  • 3
    This recipe has been scripted in [virt-delete](https://github.com/larsks/virt-utils/blob/b92333f49b4849067b6f727534d2dec47ebe3074/virt-delete) from [virt-utils](https://github.com/larsks/virt-utils), which also has a few other useful scripts. – imz -- Ivan Zakharyaschev Jan 25 '17 at 03:05
  • You can do `virsh undefine yourdomainname --storage vda` in one step. Be careful not to use `--remove-all-storage`, unless you know what you're doing. – ILMostro_7 Oct 03 '18 at 19:00
  • In my case an additional step was required: `virsh managedsave-remove aaa-db1` – olivecoder Oct 27 '20 at 09:00