Skip to content

Instantly share code, notes, and snippets.

@ianblenke
Last active March 8, 2019 16:46
Show Gist options
  • Save ianblenke/3cb54ef25ddaf4fac410 to your computer and use it in GitHub Desktop.
Save ianblenke/3cb54ef25ddaf4fac410 to your computer and use it in GitHub Desktop.
Delete any ElasticBeanstalk application versions older than a week old that are not currently in use by an environment.
awsargs="--profile aws-dev --region us-east-1"
now=$(date -j -f "%a %b %d %T %Z %Y" "$(date)" "+%s" 2>/dev/null);
oneweekago=$(expr $now - 604800);
aws elasticbeanstalk describe-application-versions $awsargs | \
jq -r '.ApplicationVersions[] | .ApplicationName + " " + .VersionLabel + " " + .DateCreated'| \
while read line ; do
fields=( $line )
utime=$(date -j -f %Y-%m-%dT%H:%M:%S "${fields[2]};" "+%s" 2>/dev/null);
if [[ $utime -lt $oneweekago ]]; then
if [ -n "$(aws elasticbeanstalk describe-environments $awsargs \
--application-name ${fields[0]} \
--version-label ${fields[1]}) | \
jq -r '.Environments[]')" ]; then
aws elasticbeanstalk delete-application-version $awsargs \
--application-name "${fields[0]}" \
--version-label "${fields[1]}"
fi
fi
done
@hellvinz
Copy link

hellvinz commented Mar 8, 2019

eb labs cleanup-versions now exists

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment