Original link: https://blog.frytea.com/archives/771/

All QEMU virtual machines can be deleted at once with the following command:
serial
qm list | awk 'NR>1 {print $1}' | xargs -I {} qm destroy {}
explain:
- The
qm listcommand gets a list of all virtual machines. -
awk 'NR>1 {print $1}'command skips the first line (header) and extracts the VMID column. -
xargs -I {} qm destroy {}command deletes each virtual machine by passing each VMID as an argument to theqm destroycommand.
NOTE: This command deletes all virtual machines, please ensure proper backup and confirmation is done before executing.
parallel
You can delete all QEMU virtual machines in parallel with the following command:
qm list | awk 'NR>1 {print $1}' | xargs -P 0 -I {} sh -c 'qm destroy {} || true'
explain:
- The
qm listcommand gets a list of all virtual machines. -
awk 'NR>1 {print $1}'command skips the first line (header) and extracts the VMID column. -
xargs -P 0 -I {} sh -c 'qm destroy {} || true'command deletes each VM by passing each VMID as an argument to theqm destroycommand.-P 0option tellsxargsto run as many processes as possible in parallel.|| trueensures that the exit status is0ifqm destroycommand fails, so other delete operations still proceed.
NOTE: This command deletes all virtual machines, please ensure proper backup and confirmation is done before executing.
This article is transferred from: https://blog.frytea.com/archives/771/
This site is only for collection, and the copyright belongs to the original author.