How to delete a docker image in Nexus3 Repo programmatically? - Sonatype Nexus Repository - Sonatype Community
How to delete a docker image in Nexus3 Repo programmatically?
Sorry for this seems to be the very basic question, but somehow I just can’t make it work.
In my most recently updated ubuntu22.04, I installed nexus3:latest (I think it is 3.78.2) so to use it as a docker repo. I used this command:
docker run -d --name nexus -p 8081:8081 -p 8082:8082 -v /opt/nexus-data:/nexus-data --restart unless-stopped sonatype/nexus3
Everything works fine: I can login to the UI, login as admin and then change password. I am create a docker hosted repo and put a docker image into this repo, and pull it, via docker push/pull and curl/API. But I could not delete it (I had this problem with my 8+ year old nexus docker container, in which I could not find a way to delete, and this is the reason I am trying to upgrade to ubuntu22.04 + latest nexus3). Chatgpt said I should do something like this:
- get the digest first. This works for me:
curl -s -u admin -X GET -I -H "Accept: application/vnd.docker.distribution.manifest.v2+json" http://<my_ip>:8082/v2/dockerepo2204/my_docker_img/manifests/latest | grep "Docker-Content-Digest"
===> Docker-Content-Digest: sha256:dd1c2b2f417713a3cfcb44af6c62fba79feb0453eccbfcd87ccf5f1d8e012997
- use this command to delete.
curl -u admin -X DELETE http://<my_ip>:8082/v2/dockerepo2204/my_docker_img/manifests/sha256:dd1c2b2f417713a3cfcb44af6c62fba79feb0453eccbfcd87ccf5f1d8e012997
This doesn’t work. Nothing returned. In fact, If I change the digest 2997 to 2999, a random digest, nothing is reported as well.
- Chatgpt then told me that I have to set '"allowDeleteApi": true'. But the way it provided, neither UI or curl way worked for me.
So my question: how do I delete a docker image in sonatype/nexus3 docker repo programmatically (not via UI) so I can automate this step? Any related API pointers or a working example would be very helpful.
Sorry if this question was asked and answered before. I did a quick search but could not find the answer. Thank you!
Follow-Up Posts
Post by mpiggott on Mar 28, 2025
What do you mean when you say nothing returned?
Post by Karl_Cai on Mar 28, 2025
Sorry, I meant that nothing is shown on the screen after running that command. I should have used the -i option for curl. I can’t see the other claim I made that changing the digests to a random value doesn’t output anything.
After more testing, it seems to me that in my nexus 3.78.2 community version I can use this:
curl -u admin:xxxxxx -X DELETE 'http://localhost:8081/service/rest/v1/components/'
or this:
curl -si -u admin:xxxxxx -X DELETE 'http://localhost:8082/v2/dockerepo2204/my_docker_image/manifests/sha256:dd1c2b2f417713a3cfcb44af6c62fba79feb0453eccbfcd87ccf5f1d8e012997'
to delete a docker image. The difference is: using the first/components, in the UI you can still see the deleted image, but the second/manifests way cleans up the UI. In both cases, the blobs are not cleaned up.
So my follow-up questions, for the goal of the original task (delete a docker image completely):
- Are these two ways both supported? If yes, what’s the difference exactly?
- More importantly, how to clean the data/blobs after these commands so I can reclaim my disk space?
Again, from Chatgpt, I got this:
To clean up:
- Wait 1 day (blobs must be unused for 24h)
- Run a “Admin - Compact blob store” task in Nexus
Is this the right step? There are so many different task types and I am not sure exactly who is what.
Thank you!
Post by brandonh on Mar 31, 2025
Hi Karl,
To fully delete the docker image from the blob store disk, you need to setup two admin tasks in the GUI to delete it fully. Read through the "Required Final Steps to Completely Delete Docker Assets From Disk" at Sonatype Support. Have those two tasks run in order to reclaim the space from the blob store disk. You might also probably want to set up a task for “Docker - Delete incomplete uploads” before running the other two as well to clean up any images that were incomplete.