Created
September 7, 2021 17:12
-
-
Save ferferga/93ca1ab3056257d05f6e33af0d6ead49 to your computer and use it in GitHub Desktop.
Deletes untagged images from GitHub Container Registry package using gh cli
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 | |
set -e | |
# Simple script to remove dangling images from GHCR. | |
# You need to have installed gh cli and jq for this script to work properly | |
# You need to be logged to 'gh' first | |
container="debian/buildd" | |
temp_file="ghcr_prune.ids" | |
rm -rf $temp_file | |
echo "Fetching dangling images from GHCR..." | |
gh api /user/packages/container/${container}/versions --paginate > $temp_file | |
ids_to_delete=$(cat "$temp_file" | jq -r '.[] | select(.metadata.container.tags==[]) | .id') | |
if [ "${ids_to_delete}" = "" ] | |
then | |
echo "There are no dangling images to remove for this package" | |
exit 0 | |
fi | |
echo -e "\nDeleting dangling images..." | |
while read -r line; do | |
id="$line" | |
## Workaround for https://github.com/cli/cli/issues/4286 and https://github.com/cli/cli/issues/3937 | |
echo -n | gh api --method DELETE /user/packages/container/${container}/versions/${id} --input - | |
echo Dangling image with ID $id deleted successfully | |
done <<< $ids_to_delete | |
rm -rf $temp_file | |
echo -e "\nAll the dangling images have been removed successfully" | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add the "--silent" option so that you don't have to be interrupted every time you call the
gh api
cli into the loopecho -n | gh api --silent --method DELETE /user/packages/container/${container}/versions/${id} --input -