Forked from ferferga/delete_ghcr_dangling_images.sh
Last active
December 31, 2024 11:58
-
-
Save matjack1/07975e89956e0644add9cec92db8ae27 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 -eux | |
# 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 | |
org=$1 | |
container=$2 | |
temp_file="ghcr_prune.ids" | |
rm -rf $temp_file | |
echo "Fetching dangling images from GHCR..." | |
gh api /orgs/${org}/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..." | |
for id in $ids_to_delete | |
do | |
gh api --method DELETE --silent /orgs/${org}/packages/container/${container}/versions/${id} | |
echo Dangling image with ID $id deleted successfully | |
done | |
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