Skip to content

Instantly share code, notes, and snippets.

@mohamed-el-habib
Created March 11, 2017 07:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save mohamed-el-habib/26d26dddaf3dcefcc0e6bdd8a15bd681 to your computer and use it in GitHub Desktop.
Save mohamed-el-habib/26d26dddaf3dcefcc0e6bdd8a15bd681 to your computer and use it in GitHub Desktop.
bash script to delete images from docker registry using search keyword
#!/bin/bash
#
# usage ./clean.docker.registry.sh registryUrl login filterString
#
# read the password
echo -n Password:
read -s password
user="$2:${password}"
dockerRegistry="$1"
imagesFilter="$3"
# get the list of images names that match the filter
images=$(curl -s -u ${user} ${dockerRegistry}/v2/_catalog | jq -r '.repositories[] | select(. | contains("'${imagesFilter}'")) ')
for image in $images ; do
# get the list of tags for each image
tags=$(curl -s -u ${user} ${dockerRegistry}/v2/${image}/tags/list | jq -r .tags[])
for tag in $tags ; do
echo "${image}:${tag}"
# get the digest of the image:tag
digest=$(curl -H "Accept: application/vnd.docker.distribution.manifest.v2+json" -v -s -u ${user} "${dockerRegistry}/v2/${image}/manifests/${tag}" 2>&1 | grep -e "Docker-Content-Digest:*" | awk '{ sub(/\r/,"",$3) ; print $3 }')
if [ -z $digest ] ; then
echo "${image}:${tag} not found"
else
echo "Deleting ${image}:${tag}:${digest}"
curl -XDELETE -w "[%{http_code}]\n" -s -u ${user} ${dockerRegistry}'/v2/'${image}'/manifests/'${digest}
echo "..."
fi
done
done
@obayit
Copy link

obayit commented Jan 25, 2021

The curl command that gets the digest is missing -I option to get the headers, currently it only outputs the response body.

@tahazayed
Copy link

Please replace "Docker-Content-Digest:" with "digest:"

@tahazayed
Copy link

@mohamed-el-habib
Kindly check my fork https://gist.github.com/tahazayed/98b7734da6dd23f8a9e167806891613d
I have added arguments and fixed the search issue

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