[SOLVED] What is the best way to delete all hard disks from a VM via command line?

joshcorreia

New Member
Nov 8, 2023
11
0
1
What is the best way to delete all hard disks from a VM via command line?

So far the only option I've been able to come up with is manually parsing output from "qm config".

Code:
$ sudo qm config 148
boot:
meta: creation-qemu=7.2.0,ctime=1700604130
name: jenkins-pipeline-28
scsi0: local:148/vm-148-disk-0.raw,size=20980890K
smbios1: uuid=f2065a25-3992-48a6-bffd-5df4113690ad
vmgenid: a549306c-431d-4fb3-a1bd-fe63e347b2fb
$ sudo qm config 148 | grep ".raw"
scsi0: local:148/vm-148-disk-0.raw,size=20980890K

Once I have the storage name, in this case "scsi0", then I can use that to remove the storage
Code:
$ qm set 148 --delete scsi0

Then I need to remove the unused storage
Code:
$ qm set 148 --delete unused0

Is there a better way to do this? Is there any use case where grepping for ".raw" won't return all of the hard disks?
 
Hi,
What is the best way to delete all hard disks from a VM via command line?

So far the only option I've been able to come up with is manually parsing output from "qm config".

Code:
$ sudo qm config 148
boot:
meta: creation-qemu=7.2.0,ctime=1700604130
name: jenkins-pipeline-28
scsi0: local:148/vm-148-disk-0.raw,size=20980890K
smbios1: uuid=f2065a25-3992-48a6-bffd-5df4113690ad
vmgenid: a549306c-431d-4fb3-a1bd-fe63e347b2fb
$ sudo qm config 148 | grep ".raw"
scsi0: local:148/vm-148-disk-0.raw,size=20980890K

Once I have the storage name, in this case "scsi0", then I can use that to remove the storage
Code:
$ qm set 148 --delete scsi0

Then I need to remove the unused storage
Code:
$ qm set 148 --delete unused0

Is there a better way to do this? Is there any use case where grepping for ".raw" won't return all of the hard disks?
If the disk is on a not file-system based storage like e.g. LVM, there is no suffix. You can match vm-XYZ-disk-N instead.

What is the use case? Removing all disks, but not the VM is not something that's commonly required.
 
If the disk is on a not file-system based storage like e.g. LVM, there is no suffix. You can match vm-XYZ-disk-N instead.

What is the use case? Removing all disks, but not the VM is not something that's commonly required.
Thanks, I changed it to grep based on ",size=" and that seems to do the trick.

The use case is that I have a CI/CD pipeline that is testing importing the .qcow2 images that I distribute. Deleting the entire VM and remaking it isn't desirable because I wish to keep the other settings the same every run, even if someone goes in the UI and changes them. If I hard code this into the creation code then changes made in the UI will be overwritten.
 
I thought i replied to this, but turns out the answer was sitting in my cookies/cache unsent.
What is the best way to delete all hard disks from a VM via command line?
I dont think there exists "the best way", mostly because this is not an operation that 99.9% execute with frequency that would justify automation.
So far the only option I've been able to come up with is manually parsing output from "qm config".
I would say that API is the more appropriate way to automate something like this, with "pvesh" being one interface that could be used
Once I have the storage name,
technically its not a "storage name" but a disk name located on "local" storage
Then I need to remove the unused storage
The man page (man qm) has several alternatives:
--force <boolean> combined with --delete
qm disk unlink <vmid> --idlist <string> [OPTIONS]
Is there a better way to do this?
I would use API.
Is there any use case where grepping for ".raw" won't return all of the hard disks?
Of course, you will only get raw if you are using "file" storage. If you are using LVM or other block storage (ie Blockbridge), there will be no "raw" in disk label.

Here is a starter:
pvesh get /cluster/resources --type storage --output-format json|jq -r '.[]|select(.content|contains("images"))|.storage'|sort|uniq
bb-iscsi
bb-nvme
local
local-lvm
nfs

The above gets a list of all possible storage objects that are capable of storing disk images, you can then query the VM config and pick out disks that contain such labels.

There should be a simpler way: pvesh get /cluster/resources --type storage --output-format json --content images
but it seems to be broken...


Blockbridge : Ultra low latency all-NVME shared storage for Proxmox - https://www.blockbridge.com/proxmox
 
There should be a simpler way: pvesh get /cluster/resources --type storage --output-format json --content images
but it seems to be broken...
It's not broken, there just is no --content option for the /cluster/resources endpoint in the first place. Since it doesn't only return storages, such an option wouldn't be a nice fit. Note there also is the /storage endpoint where it would fit, but it's questionable if it's worth adding such filtering on our end, because the API user can easily do it themselves. If you really think it's worth it, feel free to open a feature request: https://bugzilla.proxmox.com/
 

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!