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 |
In case you don't want/have the GitHub CLI:
container="my-container"
GITHUB_TOKEN="ghp_***"
while read -r id; do
curl -H "Authorization: Bearer ${GITHUB_TOKEN}" -X DELETE "https://api.github.com/user/packages/container/${container}/versions/${id}";
done <<< "$(curl https://api.github.com/user/packages/container/${container}/versions -H "Authorization: Bearer ${GITHUB_TOKEN}" | jq -r '.[] | select(.metadata.container.tags==[]) | .id')"
don't forget the shebang for bash, as pip redirection does not work with sh.
If you want to delete dangling images from an organization, replace user with orgs/myorganization
.
Thanks!
#!/bin/bash
container="mycontainer"
GITHUB_TOKEN="ghp_***"
#user="user"
user="orgs/myorganization"
page=1
while read -r id; do
echo $id
if [ "${id}" = "" ]
then
echo "There are no dangling images to remove for ${user}/${container}"
exit 0
fi
curl -H "Authorization: Bearer ${GITHUB_TOKEN}" -X DELETE "https://api.github.com/${user}/packages/container/${container}/versions/${id}";
done <<< "$(curl -s "https://api.github.com/${user}/packages/container/${container}/versions?page=${page}&per_page=100" -H "Authorization: Bearer ${GITHUB_TOKEN}" | jq -r '.[] | select(.metadata.container.tags==[]) | .id')"
Add the "--silent" option so that you don't have to be interrupted every time you call the gh api
cli into the loop
echo -n | gh api --silent --method DELETE /user/packages/container/${container}/versions/${id} --input -
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
❤️ Thanks a lot for this script !!! ❤️