Skip to content

Instantly share code, notes, and snippets.

@ferferga
Created September 7, 2021 17:12
Show Gist options
  • Save ferferga/93ca1ab3056257d05f6e33af0d6ead49 to your computer and use it in GitHub Desktop.
Save ferferga/93ca1ab3056257d05f6e33af0d6ead49 to your computer and use it in GitHub Desktop.
Deletes untagged images from GitHub Container Registry package using gh cli
#!/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
@Pandemonium1986
Copy link

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