Last active
January 15, 2020 16:32
-
-
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
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 | |
# 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