Sub topic 18
Retention and cleanup
This chapter assumes you can tag, push, and pull images, and that you know a digest identifies a manifest. It covers what to delete, how to delete it, and why deleting a tag does not always free disk space.
Why images pile up
Every build and push leaves something behind. Re-pushing myapp:latest moves the tag to a new manifest and leaves the old one with no tag. CI pipelines that tag every commit add thousands of tags per year. Layers are stored once and shared, so storage grows more slowly than tag count, but it still grows.
Cleanup happens in two places, and the tools differ:
- Your local Docker engine, which stores images and build cache on disk.
- A registry, which stores manifests and blobs (layers and configs).
Cleaning up the local engine
Start by measuring.
docker system df
docker system df -vThe summary shows space used by images, containers, volumes, and build cache, and how much of it is reclaimable.
A dangling image is an image with no tag, usually the previous build of a tag you just rebuilt. Remove dangling images with:
docker image pruneAdd -a to remove every image not used by at least one container. That includes tagged images you might want later, so pair it with a time filter:
docker image prune -a --filter "until=168h"168h is seven days. The filter applies to image creation time, not last use.
Build cache is separate from images:
docker builder prune
docker builder prune --filter "until=72h"The first command removes dangling build cache. Adding --all also removes cache that other builds could reuse. Pruning the cache makes the next build slower, because the layers you cached in the [build cache chapter](#) must be rebuilt.
Tags, manifests, and blobs
Registry cleanup is harder because the registry stores three kinds of objects that depend on each other.
- A tag is a name that points to a manifest digest.
- A manifest lists a config blob and layer blobs by digest.
- A blob is stored once, no matter how many manifests reference it.
Removing a tag removes only the pointer. The manifest may remain, and so do its blobs. A blob can be deleted only when no remaining manifest references it. Deciding that requires a sweep over all manifests, which is garbage collection.
In the diagram, deleting manifest aaa and collecting garbage frees layer B only. Layer A stays because manifest bbb still references it.
Deleting from a self-hosted registry
The open-source registry:2 image refuses deletes by default. Enable them with an environment variable:
docker run -d -p 5000:5000 --name registry \
-e REGISTRY_STORAGE_DELETE_ENABLED=true registry:2Deleting through the HTTP API needs the manifest digest, not the tag. Read it from the Docker-Content-Digest response header. The Accept header must name the manifest media type, or the registry may return a different digest than the one you pushed.
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/sha256:<digest>Replace <digest> with the value from the first command. A successful delete returns 202 Accepted. At this point the blobs are still on disk. Run garbage collection to remove them, with a dry run first:
docker exec registry bin/registry garbage-collect --dry-run /etc/docker/registry/config.yml
docker exec registry bin/registry garbage-collect /etc/docker/registry/config.ymlAdd --delete-untagged to also remove manifests that no tag points to.
Deleting by digest removes every tag
Several tags can point to one manifest. Deleting the manifest by digest makes all of them fail to resolve. Check before you delete. A release process that tags the same build as 1.4.2, 1.4, and latest has three tags on one digest.
This matters for [digest pinning](#) too. If a deployment pins myapp@sha256:... and you delete that manifest, the deployment cannot pull a replacement node or restart on a fresh host. Keep every digest that production references.
Retention policies on hosted registries
Hosted registries such as Amazon ECR, Google Artifact Registry, GitHub Container Registry, and Docker Hub provide automated retention. The details differ by product, so check your provider's documentation. The shape is the same: a rule selects images by tag status, tag pattern, or age, and an action expires them. Amazon ECR calls these lifecycle policies:
{
"rules": [
{
"rulePriority": 1,
"description": "Expire untagged images after 14 days",
"selection": {
"tagStatus": "untagged",
"countType": "sinceImagePushed",
"countUnit": "days",
"countNumber": 14
},
"action": { "type": "expire" }
},
{
"rulePriority": 2,
"description": "Keep the 20 most recent release images",
"selection": {
"tagStatus": "tagged",
"tagPrefixList": ["v"],
"countType": "imageCountMoreThan",
"countNumber": 20
},
"action": { "type": "expire" }
}
]
}Apply it to a repository with:
aws ecr put-lifecycle-policy --repository-name team/myapp --lifecycle-policy-text file://policy.jsonHosted registries collect unreferenced blobs for you, so you do not run a garbage collector.
Choosing a policy
A workable policy answers these questions:
- Which tags are releases? Keep them for as long as you must be able to roll back or reproduce a deployment.
- Which tags are throwaway, such as per-branch or per-commit tags? Expire them by age, for example 14 to 30 days.
- Are digests referenced anywhere outside the registry, such as in deployment manifests, SBOMs, or signatures? Never expire those.
- Does the repository hold multi-platform images? Deleting an image index while per-platform manifests remain, or the reverse, can leave orphans or break a pull for one architecture. Prefer provider rules that treat the index and its children together.
Dry-run any rule before enabling it. ECR offers a lifecycle policy preview, and the self-hosted collector has --dry-run.
Common errors
| Symptom | Likely cause |
|---|---|
| Registry delete returns `405 Method Not Allowed` | Deletes are not enabled in the registry storage configuration |
| Delete by tag returns `400` or `404` | The API needs a digest, not a tag |
| Disk usage unchanged after deleting tags | Garbage collection has not run, or other manifests share the blobs |
| `manifest unknown` on pull after cleanup | A rule or manual delete removed a manifest that a deployment still references |
| `docker image prune -a` removed an image you wanted | It removes every image with no container using it, not only old ones |
Exercise
You delete the tag myapp:1.0 from your self-hosted registry, then run docker system df on the registry host and see no change in disk usage. Name two reasons this can happen and one action that addresses the first.
Show answer
The registry stores manifests and blobs separately from tags. Deleting a tag, or even its manifest, does not remove blobs until garbage collection runs. Also, the layers may be shared with other manifests such as myapp:2.0, so they are still referenced and must stay. To address the first reason, enable deletes (REGISTRY_STORAGE_DELETE_ENABLED=true), delete the manifest by digest, and run bin/registry garbage-collect while the registry is read-only or stopped. Note also that docker system df shows local engine usage, not the registry's storage, so check the registry's own volume.
Exercise
A CI pipeline pushes a tag for every commit, and releases are tagged v1.2.3. Describe a retention policy that keeps rollbacks possible and limits growth.
Show answer
Keep release tags (prefix v) with a count rule such as the most recent 20, or indefinitely if compliance requires it. Expire untagged images after about 14 days. Expire per-commit tags by age, for example 30 days. Exclude any digest referenced by running deployments. Preview the rules with a dry run before turning them on.