Created
June 23, 2022 15:15
-
-
Save varunchandak/3ab38590d7e3e83db5c83efaeeb7828a to your computer and use it in GitHub Desktop.
Script to delete unused disks from GCP project
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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