Skip to content

Instantly share code, notes, and snippets.

@woolfg
Last active January 15, 2020 16:32
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 woolfg/9e5c6e4e3542050b8a6903add29b5f98 to your computer and use it in GitHub Desktop.
Save woolfg/9e5c6e4e3542050b8a6903add29b5f98 to your computer and use it in GitHub Desktop.
Cleanup GCP container registry and delete all tagged images but the latest one
#!/bin/bash
# this script lists all images and available tags and proposes commands to delete older images
# adapt the registry uri of the gcp container registry
registry=eu.gcr.io/YOUR_PROJECT_ID
NL=$'\n'
echo "fetching repos..."
repos=$(gcloud container images list --repository $registry | tail -n +2)
for repo in ${repos};
do
echo "fetching tags for ${repo}:"
tags=$(gcloud container images list-tags "${repo}")
echo "${tags}";
echo "---"
echo "to remove all images but the newest one:";
for tag in $(echo "${tags}" | tail -n +3 | cut -f 1 -d ' ');
do
del="gcloud container images delete ${repo}@sha256:${tag} --force-delete-tags --quiet;"
echo "${del}"
alldel="${alldel}${NL}${del}"
done;
echo "+++++++++++++++++++++++++++++"
echo ""
done;
echo "==== All delete commands ===="
echo "${alldel}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment