Skip to content

Instantly share code, notes, and snippets.

@matzegebbe
Last active March 22, 2024 06:43
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save matzegebbe/a2678b227add6bafad9a3a802618b5ad to your computer and use it in GitHub Desktop.
Save matzegebbe/a2678b227add6bafad9a3a802618b5ad to your computer and use it in GitHub Desktop.
Nexus Repository Manager keep the last X docker images delete all other
#!/bin/bash
REPO_URL="https://repository.xxx.net/repository/"
USER="admin"
PASSWORD="datpassword"
BUCKET="portal-docker"
KEEP_IMAGES=10
IMAGES=$(curl --silent -X GET -H 'Accept: application/vnd.docker.distribution.manifest.v2+json' -u ${USER}:${PASSWORD} "${REPO_URL}${BUCKET}/v2/_catalog" | jq .repositories | jq -r '.[]' )
echo ${IMAGES}
for IMAGE_NAME in ${IMAGES}; do
echo ${IMAGE_NAME}
TAGS=$(curl --silent -X GET -H 'Accept: application/vnd.docker.distribution.manifest.v2+json' -u ${USER}:${PASSWORD} "${REPO_URL}${BUCKET}/v2/${IMAGE_NAME}/tags/list" | jq .tags | jq -r '.[]' )
TAG_COUNT=$(echo $TAGS | wc -w)
let TAG_COUNT_DEL=${TAG_COUNT}-${KEEP_IMAGES}
COUNTER=0
echo "THERE ARE ${TAG_COUNT} IMAGES FOR ${IMAGE_NAME}"
## skip if smaller than keep
if [ "${KEEP_IMAGES}" -gt "${TAG_COUNT}" ]
then
echo "There are only ${TAG_COUNT} Images for ${IMAGE_NAME} - nothing to delete"
continue
fi
for TAG in ${TAGS}; do
let COUNTER=COUNTER+1
if [ "${COUNTER}" -gt "${TAG_COUNT_DEL}" ]
then
break;
fi
IMAGE_SHA=$(curl --silent -I -X GET -H 'Accept: application/vnd.docker.distribution.manifest.v2+json' -u ${USER}:${PASSWORD} "${REPO_URL}${BUCKET}/v2/${IMAGE_NAME}/manifests/$TAG" | grep Docker-Content-Digest | cut -d ":" -f3 | tr -d '\r')
echo "DELETE ${TAG} ${IMAGE_SHA}";
DEL_URL="${REPO_URL}${BUCKET}/v2/${IMAGE_NAME}/manifests/sha256:${IMAGE_SHA}"
RET="$(curl --silent -k -X DELETE -H 'Accept: application/vnd.docker.distribution.manifest.v2+json' -u ${USER}:${PASSWORD} $DEL_URL)"
done;
done;
@gvisca
Copy link

gvisca commented Apr 13, 2018

Hello,
Thanks a lot for this gist, it was exactly what I was looking for !

Anyway I'm facing an issue with the storage on disk because the image is deleted in nexus (not visible in the UI) but the disk space does not decrease
Is there a way to really remove the image and layers from disk ? Am I missing a parameter in my Nexus configuration to really remove the images ?

My nexus version is 3.10 and even after running the "Docker - Delete unused manifests and images" task the disk space remains the same.

Thanks for your help.

@satyam88
Copy link

Hello,

Can you please explain line 10 " jq .repositories | jq -r "

./nexuscleanup.sh: line 12: jq: command not found
./nexuscleanup.sh: line 12: jq: command not found
(23) Failed writing body

I got above error

@vijay880755
Copy link

Hello,

Can you please explain line 10 " jq .repositories | jq -r "

./nexuscleanup.sh: line 12: jq: command not found
./nexuscleanup.sh: line 12: jq: command not found
(23) Failed writing body

I got above error

Install jq on the machine

@ddurham2
Copy link

ddurham2 commented Jan 3, 2024

Hello, Thanks a lot for this gist, it was exactly what I was looking for !

Anyway I'm facing an issue with the storage on disk because the image is deleted in nexus (not visible in the UI) but the disk space does not decrease Is there a way to really remove the image and layers from disk ? Am I missing a parameter in my Nexus configuration to really remove the images ?

My nexus version is 3.10 and even after running the "Docker - Delete unused manifests and images" task the disk space remains the same.

Thanks for your help.

You may need to consider running a "Compact blob store" task -- https://help.sonatype.com/repomanager3/nexus-repository-administration/repository-management/cleanup-policies?_ga=2.95007263.285540370.1704304621-1985157648.1703700486

@jav-12
Copy link

jav-12 commented Mar 22, 2024

How can I add a filter to remove images that are older than two weeks?

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