简体   繁体   中英

Delete all stopped instances from Google Compute Engine

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!

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}' 
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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM