This is doable, although ugly. You need to be running (I think) registry 2.3 or greater, and have enabled deleting (REGISTRY_STORAGE_DELETE_ENABLED=True
env var or equivalent). The example commands below assume a local filestore in /srv/docker-registry
, but I'd be surprised if something equivalent couldn't be cooked up for other storage backends.
For each repository you wish to tidy up, you need to enumerate the digest references that are no longer required. The easiest way to do this is per-tag, using latest
as an example in this case, you'd do something like:
ls -1tr /srv/docker-registry/v2/repositories/<repo>/_manifests/tags/latest/index/sha256 \
| tail -n +3
This will list all but the three most recent digests pushed to the latest
tag. Alternately, if you don't care about tags so much, but just want to keep the last few references pushed, you can do:
ls -1t /srv/docker-registry/v2/repositories/<repo>/_manifests/revisions/sha256 \
| tail -n +3
Then, you just delete the references you don't need:
for hash in $(ls -1t /srv/docker-registry/v2/repositories/<repo>/_manifests/tags/latest/index/sha256 | tail -n +3); do
curl -X DELETE https://registry:5000/v2/<repo>/manifests/sha256:$hash
done
Finally, you need to do a GC run, because the registry implements "soft deletes", which doesn't actually delete anything, it just makes it unavailable:
docker exec docker-registry /bin/registry \
garbage-collect /path/to/config.yml
Yes, this is all messy as hell, grovelling around in the backend storage, because there's no API method to enumerate all digests associated with a given tag, but that's the way the cookie crumbles.