Skip to content

Instantly share code, notes, and snippets.

@rifatx
Last active May 30, 2018 12:58
Show Gist options
  • Save rifatx/8d106bdc70a681cefa07e3f6b5954c61 to your computer and use it in GitHub Desktop.
Save rifatx/8d106bdc70a681cefa07e3f6b5954c61 to your computer and use it in GitHub Desktop.
shell script to clean all but last N images in gcr
#!/bin/bash
repoAddress="xx.gcr.io/xXx"
repos=$(gcloud container images list --format='table[no-heading](name:sort=1)' --repository=repoAddress)
while read -r repo; do
images=$(gcloud container images list-tags --limit=99999 --sort-by=~timestamp --format='get(digest)' $repo)
n=10
while read -r image; do
if (( n < 1 )); then
gcloud container images delete --quiet "$repo@$image"
fi;
n=$((n-1))
done <<< "$images"
done <<< "$repos"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment