Reputation: 937
I have some thousands of VM instances on Google Compute Engine. Almost all of them are stopped. How can I delete all the stopped instances at once? (doing so on the UI will take ages, and furthermore - the UI crashes)
Thanks!
Upvotes: 1
Views: 2983
Reputation: 32179
terminated instances show in the gcloud console as stopped. assuming you wish to delete terminated instances, you can look for instances which have a status of TERMINATED.
the other answers here will work but they will iterate through all of your instances. a slightly cleaner approach is to request a filtered instance list from gcloud so that you only iterate through instances which are already known to be in this state.
finally, i've found that secondary disks don't always delete when their parent instance deletes, so i also like to purge orphaned disks during cleanup.
something like this should do it (for a bash shell):
#!/bin/bash
# delete terminated instances
for terminated_instance_uri in $(gcloud compute instances list --uri --filter="status:TERMINATED" 2> /dev/null); do
terminated_instance_name=${terminated_instance_uri##*/}
terminated_instance_zone_uri=${terminated_instance_uri/\/instances\/${terminated_instance_name}/}
terminated_instance_zone=${terminated_instance_zone_uri##*/}
if [ -n "${terminated_instance_name}" ] && [ -n "${terminated_instance_zone}" ] && gcloud compute instances delete ${terminated_instance_name} --zone ${terminated_instance_zone} --delete-disks all --quiet; then
echo "deleted: ${terminated_instance_zone}/${terminated_instance_name}"
fi
done
# delete orphaned disks (filter for disks without a user)
for orphaned_disk_uri in $(gcloud compute disks list --uri --filter="-users:*" 2> /dev/null); do
orphaned_disk_name=${orphaned_disk_uri##*/}
orphaned_disk_zone_uri=${orphaned_disk_uri/\/disks\/${orphaned_disk_name}/}
orphaned_disk_zone=${orphaned_disk_zone_uri##*/}
if [ -n "${orphaned_disk_name}" ] && [ -n "${orphaned_disk_zone}" ] && gcloud compute disks delete ${orphaned_disk_name} --zone ${orphaned_disk_zone} --quiet; then
echo "deleted: ${orphaned_disk_zone}/${orphaned_disk_name}"
fi
done
Upvotes: 1
Reputation: 55
yes Y | gcloud compute instances list | awk '/TERMINATE/ {printf "gcloud compute instances delete %s --zone %s; ", $1, $2}' | bash
gcloud compute instances list : list instances one by one.
awk: prints out "gcloud compute instances delete "terminated_instance_name" --zone "zone name that instance belongs to"
Pipe this output to bash to make it execute on terminal;
And "yes Y" supplies a 'Y' or yes answer when prompted for confirmation.
Upvotes: 1
Reputation: 91
First get the list of VMs from your project:
gcloud compute instances list | grep TERMINATE
Verify that all these VMs need to be deleted. Then following generates the commands you can execute to delete them all. You can redirect the output to a file and then run "bash ". Feel free to optimize to a single command line if you are feeling lucky :)
gcloud compute instances list | grep TERMINATE | awk '{printf "gcloud comoute instances delete %s --zone %s\n", $1, $2}'
Upvotes: 3