how to delete or destroy vm

chalex

Active Member
Mar 19, 2018
22
1
43
41
New stock install of PVE.

I created a couple of test VMs; things work fine. Now I would like to delete them.

Somehow I don't find any "delete" or "destroy" option in the Web UI. And also not in the PDF guide, or in the forum search.

What is the procedure to delete a VM? Delete the config file from /etc/pve and then manually delete the LV?
 
New stock install of PVE.

I created a couple of test VMs; things work fine. Now I would like to delete them.

Somehow I don't find any "delete" or "destroy" option in the Web UI. And also not in the PDF guide, or in the forum search.

What is the procedure to delete a VM? Delete the config file from /etc/pve and then manually delete the LV?

==================
Answer: You can delete or destroy by stop it HA first if you have enabled HA and then you can delete by select on VM --> More (Select Dropdown list--> Remove)
 
Try detaching the VM storage/hard disk first from the VM's Hardware tab. If you want also to delete the virtual disk, you can select the unused disk and then select remove.
 
Last edited:
If you want to delete multiple VMs you could create a script like:

Code:
#!/bin/bash
vmids=( 10000 11000 20000 ) # VMIDs of VMs to delete
for vmid in "${vmids[@]}"
do
    status=$(qm status "$vmid")
    if [ "$status" = "status: running" ]; then
      qm stop $vmid
    fi
    qm destroy $vmid
done
 
Last edited:
  • Like
Reactions: djsami and cool
If you want to delete multiple VMs you could create a script like:

Code:
#!/bin/bash
vmids=( 10000 11000 20000 ) # VMIDs of VMs to delete
for vmid in "${vmids[@]}"
do
    status=$(qm status "$vmid")
    if [ "$status" = "status: running" ]; then
      qm stop $vmid
    fi
    qm destroy $vmid
done

Hello

vmids=( 10000 11000 20000 ) # VMIDs of VMs to delete



We are writing the vmm id that will be deleted in this part, right?

100 105 104 103 102 540 320 450 678 vs...

how do we do it this way?

./deletevm ?

sh deletevm ?
 
Last edited:
Hello

vmids=( 10000 11000 20000 ) # VMIDs of VMs to delete



We are writing the vmm id that will be deleted in this part, right?

100 105 104 103 102 540 320 450 678 vs...

how do we do it this way?

./deletevm ?

sh deletevm ?
bash deletevm.sh
 
Yes, "vmids=( 10000 11000 20000 )" is an array of VMIDs. It will iterate through all VMIDs and check if the VM is running. If it is running it will be stopped, as running VMs can't be deleted. Then it will destroy that VM.

It was just a simple example. If you really want to use something like that there could be added more stuff. For example that script could be made interactive so you can type in the list of VMID to delete. Then a confirmation that is required to type in so no one deletes stuff by accident. And in case LXCs are used too, there should be a check if the VMID is belonging to a VM or LXC. Because for LXCs you need to run different commands ("pct" instead of "qm").
 
Last edited:
Here is a better versionwith the stuff I mentioned above:
Code:
#!/bin/bash
# ask user which VMIDs to destory
read -p "Enter VMIDs of VMs/LXCs to destroy separated by spaces: " -a vmids
# check if all VMIDs exist and store them differentiated as LXCs or VMs
vms=()
lxcs=()
problems=()
for vmid in "${vmids[@]}"
do
    # check that VMID is an integer
    if [ -z "${vmid##*[!0-9]*}" ]; then
        problems+=($vmid)
    else
        # check if vmid is a VM
        check=$(qm status $vmid > /dev/null 2>&1)
        if [ $? -eq 0 ]; then
            vms+=($vmid)
        else
            # check if vmid is a LXC
            check=$(pct status $vmid > /dev/null 2>&1)
            if [ $? -eq 0 ]; then
                lxcs+=($vmid)
            else
                problems+=($vmid)
            fi
        fi
    fi
done
echo "Following guests will be destroyed."
if [ ${#vms[@]} -gt 0 ]; then
    echo "VMs:"
    first=0
    for vmid in "${vms[@]}"
    do
        # show list of VMs with names and VMIDs
        name=$(qm status ${vmid} --verbose yes | grep "name: " | cut -d" " -f 2)
        if [ $first -eq 0 ]; then
            echo -n "${name}(${vmid})"
            first=1
        else
            echo -n ", ${name}(${vmid})"
        fi
    done
    echo ""
fi
if [ ${#lxcs[@]} -gt 0 ]; then
    echo "LXCs:"
    first=0
    for vmid in "${lxcs[@]}"
    do
        # show list of LXCs with names and VMIDs
        name=$(pct status ${vmid} --verbose yes | grep "name: " | cut -d" " -f 2)
        if [ $first -eq 0 ]; then
            echo -n "${name}(${vmid})"
            first=1
        else
            echo -n ", ${name}(${vmid})"
        fi
    done
    echo ""
fi
if [ ${#problems[@]} -gt 0 ]; then
    echo "Problems with the following VMIDs, so they will be skipped:"
    echo "${problems[@]}"
fi
# ask for confirmation before destroying anything
read -p "Do you really want to destroy all of these VMs/LXCs? There is no way to get them back! Please type 'DESTROY' to confirm: " confirm
if [ "$confirm" = "DESTROY" ]; then
    echo "Start destroying VMs/LXCs..."
    for vmid in "${vms[@]}"
    do
        status=$(qm status "$vmid")
        if [ "$status" = "status: running" ]; then
            qm stop $vmid
            if [ $? -ne 0 ]; then
                echo "Error: VM with VMID ${vmid} was running but couldn't be stopped, so can't destroy it!"
            fi
        fi
        if [ "$status" = "status: stopped" ]; then
            qm destroy $vmid
            if [ $? -ne 0 ]; then
                echo "Error: Failed to destroy VM with VMID ${vmid}!"
            fi
        fi
    done
    for vmid in "${lxcs[@]}"
    do
        status=$(pct status "$vmid")
        if [ "$status" = "status: running" ]; then
            pct stop $vmid
            if [ $? -ne 0 ]; then
                echo "Error: LXC with VMID ${vmid} was running but couldn't be stopped, so can't destroy it!"
            fi
        fi
        if [ "$status" = "status: stopped" ]; then
            pct destroy $vmid
            if [ $? -ne 0 ]; then
                echo "Error: Failed to destroy LXC with VMID ${vmid}!"
            fi
        fi
    done
    echo "Destroying finished."
else  
    echo "Destroying aborted."
fi

I saved it as file "destroyguests.bash" and made it executable. You can then run it with bash /path/to/script/destroyguests.bash. It will look like this:
Code:
root@Hypervisor:~/scripts# bash destroyguests.bash
Enter VMIDs of VMs/LXCs to destroy separated by spaces: 102 106 109 110 115 121 133 136 137 138 9999 foo
Following guests will be destroyed.
VMs:
OPNsense(102), Emby(106), WebGateway(109), Nextcloud(110), digiKam(115)
LXCs:
GraylogLXC(121), DokuWiki(133), ZabbixLXC(136), PiholeLXC(137), PiholeLXC2(138)
Problems with the following VMIDs, so they will be skipped:
9999 foo
Do you really want to destroy all of these VMs/LXCs? There is no way to get them back! Please type 'DESTROY' to confirm: DESTROY
Start destroying VMs/LXCs...
Destroying finished.
First it will ask for a space separated list of VMIDs. Can either be VMs or LXCs. Then it will check if they exist and list all the VMs and LXCs that with theit names that will be destroyed.
You then have to confirm it by typing "DESTROY". Then it will stop the VMs/LXCs in case they are running and destroy them.
 

About

The Proxmox community has been around for many years and offers help and support for Proxmox VE, Proxmox Backup Server, and Proxmox Mail Gateway.
We think our community is one of the best thanks to people like you!

Get your subscription!

The Proxmox team works very hard to make sure you are running the best software and getting stable updates and security enhancements, as well as quick enterprise support. Tens of thousands of happy customers have a Proxmox subscription. Get yours easily in our online shop.

Buy now!