Skip to content

Instantly share code, notes, and snippets.

@tahaozket
Forked from browny/gce-instance-clean.sh
Created May 3, 2021 13:48
Show Gist options
  • Save tahaozket/b0b80e573f689603da028afbad75a88d to your computer and use it in GitHub Desktop.
Save tahaozket/b0b80e573f689603da028afbad75a88d to your computer and use it in GitHub Desktop.
# install parallel first (sudo apt-get install -y parallel)
JOB_NUM=10
NAME_FILTER='*'
# stop instances
gcloud compute instances list --filter=name:$NAME_FILTER | awk '/RUNNING/ {printf "gcloud -q compute instances stop %s --zone %s;\n", $1, $2 > "stop-instances.txt" }'
parallel --jobs $JOB_NUM < stop-instances.txt
# remove delete protection
gcloud compute instances list --filter=name:$NAME_FILTER | awk '/TERMINATED/ {printf "gcloud -q compute instances update %s --no-deletion-protection --zone %s;\n", $1, $2 > "remove-protection.txt" }'
parallel --jobs $JOB_NUM < remove-protection.txt
# delete instances
gcloud compute instances list --filter=name:$NAME_FILTER | awk '/TERMINATED/ {printf "gcloud -q compute instances delete %s --zone %s;\n", $1, $2 > "delete-instances.txt" }'
parallel --jobs $JOB_NUM < delete-instances.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment