Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save varunchandak/3ab38590d7e3e83db5c83efaeeb7828a to your computer and use it in GitHub Desktop.
Save varunchandak/3ab38590d7e3e83db5c83efaeeb7828a to your computer and use it in GitHub Desktop.
Script to delete unused disks from GCP project
export GCP_PROJECT=<PROJECT_ID>
IFS=','
gcloud compute disks list \
--format json \
--project "$GCP_PROJECT" | \
jq -r '.[]|select(.users == null)|.name, .zone' | \
paste -d, - - | \
sed "s,https://www.googleapis.com/compute/v1/projects/$GCP_PROJECT/zones/,,g" | \
while read DISK_NAME ZONE_ID; do
gcloud compute disks delete "$DISK_NAME" \
--zone "$ZONE_ID" \
--quiet \
--project "$GCP_PROJECT"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment