Created
October 16, 2020 17:50
-
-
Save evansmwendwa/78f7d65d448a01f8325e23b98704a32a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/bin/bash | |
# A bash script to remove old versions of a Google App Engine instance. | |
# | |
# Inspiration of script taken from: | |
# https://almcc.me/blog/2017/05/04/removing-older-versions-on-google-app-engine/ | |
# Original code by Alastair McClelland and Marty Číž. | |
# Assembled and modified by Johan Niklasson. | |
# | |
# To run this script, execute | |
# sh remove_old_gcloud_versions.sh <instance> <versions to keep> | |
# where <instance> is the instance type to filter (usuaylly default) | |
# and <versions to keep> is the number of versions to keep. | |
# The type of instance to filter on (usually default) | |
INSTANCE_TYPE=$1 | |
# How many versions to keep, and throw away the rest | |
VERSIONS_TO_KEEP=$(($2 + 1)) | |
# If you run a flexible environment, run this script instead as it will make sure that the instances are stopped before trying to remove them | |
#VERSIONS=$(gcloud app versions list --service $INSTANCE_TYPE --sort-by '~version' --filter="version.servingStatus='STOPPED'" --format 'value(version.id)' | sort -r | tail -n +$VERSIONS_TO_KEEP | paste -sd " " -) | |
# If you run the standard environment, you can't stop services and will always get an empty result if you run the command above. | |
VERSIONS=$(gcloud app versions list --service $INSTANCE_TYPE --sort-by '~version' --format 'value(version.id)' | sort -r | tail -n +$VERSIONS_TO_KEEP | paste -sd " " -) | |
# Don't try to delete old versions if there were no from the filtering. | |
if [ ${#VERSIONS} -gt 0 ] | |
then | |
# If you want to confirm before deletion, remove the -q | |
gcloud app versions delete --service $INSTANCE_TYPE $VERSIONS -q | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment