Sub topic 18
Retention and cleanup
This chapter assumes you can build, tag, push, and pull images, and that you know a tag is a name pointing at a manifest identified by a digest. Images pile up in two places: on your machine and in the registry. Each needs its own cleanup.
Cleaning up locally
Check disk use first:
docker system df
docker image prune
docker image prune -a --filter "until=720h"docker image prune removes dangling images. These are untagged layers, usually left behind when a rebuild moves a tag to a new image. Adding -a also removes every image not used by a container. The until filter limits that to images created more than 720 hours (30 days) ago.
Why registry cleanup is different
A tag is only a pointer, so deleting one does not free space. The space is held by manifests and layer blobs. A blob can be shared by many images, so a registry can remove it only after nothing references it. This takes two steps: delete the manifest, then garbage-collect the unreferenced blobs.
Cleaning a local registry
The registry:2 image from the earlier chapter refuses deletes unless you enable them:
docker run -d -p 5000:5000 -e REGISTRY_STORAGE_DELETE_ENABLED=true --name registry registry:2
docker tag myapp:1.0 localhost:5000/myapp:1.0
docker push localhost:5000/myapp:1.0Look up the manifest digest, then delete by digest. The registry does not accept deletes by tag.
curl -sI -H "Accept: application/vnd.oci.image.manifest.v1+json, application/vnd.docker.distribution.manifest.v2+json" \
http://localhost:5000/v2/myapp/manifests/1.0 | grep -i docker-content-digest
curl -X DELETE http://localhost:5000/v2/myapp/manifests/<digest>
docker exec registry registry garbage-collect /etc/docker/registry/config.ymlReplace <digest> with the value from the first command. Add --delete-untagged to the last command to also remove manifests that no tag points to.
Gotchas
- Deleting by digest removes every tag that points to that manifest.
- For a multi-platform image, delete the index and its per-platform manifests together. Otherwise untagged manifests stay behind.
- Anyone who pinned the digest will get a "not found" error on the next pull.
- Garbage collection on the open-source registry should run while the registry is read-only or stopped. A push that runs at the same time can lose layers it just uploaded.
Retention policies
Hosted registries automate this with rules, such as "keep the last 10 tags matching release-*" or "expire untagged images after 14 days". Amazon ECR calls these lifecycle policies, and other registries offer similar rules. A sensible policy keeps release tags and anything currently deployed. It expires CI build tags and untagged manifests on a schedule.
Exercise
You run curl -X DELETE on a manifest in a local registry, but disk usage does not change. What step is missing, and why?
Show answer
Garbage collection. Deleting the manifest only removes the reference. The layer blobs stay on disk until registry garbage-collect removes the ones no manifest references. Also check that the registry was started with REGISTRYSTORAGEDELETE_ENABLED=true, or the delete itself will be refused.