Skip to content

Instantly share code, notes, and snippets.

@qoomon
Forked from bjaglin/remove-orphan-images.sh
Last active November 21, 2019 20:04
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save qoomon/7c7f16939630cafafceeb83d254194e4 to your computer and use it in GitHub Desktop.
Save qoomon/7c7f16939630cafafceeb83d254194e4 to your computer and use it in GitHub Desktop.
DEPRECATED in favour of `docker image prune` https://docs.docker.com/config/pruning/
#!/bin/sh
base_dir=/var/lib/registry/docker/registry/v2
repository_dir=$base_dir/repositories
image_dir=$base_dir/blobs
output_dir=$(mktemp -d)
all_images=$output_dir/all
used_images=$output_dir/used
unused_images=$output_dir/unused
find "$image_dir/sha256/" -mindepth 2 -type d -exec basename {} \; > "$all_images"
echo "Collecting orphan images">&2
for library in $repository_dir/*; do
echo "Library $(basename "$library")" >&2
for repo in $library/*; do
echo " Repo $(basename "$repo")" >&2
for tag in $repo/_manifests/tags/*; do
echo " Tag $(basename "$tag")" >&2
image_hash=$(basename $tag/index/sha256/*)
echo " Image $image_hash" >&2
echo "$image_hash"
jq -r '.config.digest' "$image_dir/sha256/${image_hash:0:2}/$image_hash/data" | sed 's|sha256:||'
jq -r '.layers[].digest' "$image_dir/sha256/${image_hash:0:2}/$image_hash/data" | sed 's|sha256:||'
done
done
done | sort | uniq > "$used_images"
grep -v -F -f "$used_images" "$all_images" > "$unused_images"
all_image_count=$(wc -l < "$all_images")
used_image_count=$(wc -l < "$used_images")
unused_image_count=$(wc -l < "$unused_images")
unused_image_size='0'
echo "${all_image_count} images, ${used_image_count} used, ${unused_image_count} unused"
if [ "$unused_image_count" -gt 0 ]; then
unused_image_folders=""
for image_hash in $(cat "$unused_images"); do
unused_image_folders="$unused_image_folders $image_dir/sha256/${image_hash:0:2}/$image_hash"
done
unused_image_size=$(du -hc $unused_image_folders | tail -n1 | cut -f1)
echo "Unused images consume ${unused_image_size}"
echo "Delete unused Images? [y/n]"
read confirmation
if [ "$confirmation" == "y" ]; then
echo "Cleanup images"
for image_hash in $(cat "$unused_images"); do
rm -rf "$image_dir/sha256/${image_hash:0:2}/$image_hash"
done
fi
fi
@Grief
Copy link

Grief commented Apr 30, 2017

LANG=C sudo ./docker-registry-clean.sh

find: warning: you have specified the -mindepth option after a non-option argument -type, but options are not positional (-mindepth affects tests specified before it as well as those specified after it).  Please specify options before other arguments.

find: ‘/var/lib/registry/docker/registry/v2/blobs/sha256/’: No such file or directory
Collecting orphan images
Library *
 Repo *
  Tag *
   Image *
./docker-registry-clean.sh: 29: ./docker-registry-clean.sh: Bad substitution
./docker-registry-clean.sh: 30: ./docker-registry-clean.sh: Bad substitution
0 images, 1 used, 0 unused

@Arglanir
Copy link

Thank you for this script.
I changed a little: image_hash=$(ls -t $tag/index/sha256 | grep -v total | head -1) (you may have multiple (old) images for one given tag)

@gael-jurin
Copy link

Many thanks for the job ! For someone who wants to use it, just be careful about the shell sh / bash.

@wilsonmar
Copy link

@gael-jurin what do you mean by "shell sh/bash"?

@qoomon
Copy link
Author

qoomon commented Nov 21, 2019

I think this script is deprecated, in favour of docker image prune https://docs.docker.com/config/pruning/

@gael-jurin
Copy link

gael-jurin commented Nov 21, 2019

@gael-jurin what do you mean by "shell sh/bash"?

I mean not paying attention about the first line of the script and try to run it with a default bash shell.
@qoomon Right ! but still doing the job if someone want to customize the operation. Thx !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment