Sub topic 6
Pruning the build cache
This chapter assumes you can run docker build, know that an image is built from layers, and have already used docker system df to see where space goes.
What the build cache is
When you run docker build, Docker saves the result of each build step. If a step and its inputs have not changed, the next build reuses the saved result instead of running the step again. These saved results are the build cache.
The cache makes rebuilds fast. It also grows quietly. Every changed RUN, COPY, or base image can leave behind old cache entries that no later build will use. On a busy build machine or CI runner, the cache is often the largest item in docker system df.
Seeing how big it is
Check the Build Cache row of the summary:
docker system df
docker system df -vThe summary shows the total size and how much is reclaimable. The verbose output lists the individual cache records, and you can see which ones are in use.
Removing unused cache
The matching prune command is docker builder prune:
docker builder pruneDocker asks for confirmation, then deletes unused build cache and prints the space it reclaimed.
The default is cautious. It removes only dangling cache, meaning records that are not tied to anything a later build could still use. It does not remove all cache that is merely idle. To remove every cache record that no build is using right now, add -a:
docker builder prune -aLimiting what gets removed
A full wipe is often more than you need. Use the until filter to keep recent cache and delete only old records:
docker builder prune -a --filter "until=168h"This removes unused cache not touched in the last 168 hours (7 days). Active projects keep their fast rebuilds, and stale entries go away.
Another option is to keep a fixed amount of cache and remove the rest, oldest first:
docker builder prune --keep-storage 5GBThis suits build servers, because the cache stays warm but cannot grow without limit. Run docker builder prune --help to confirm which flags your Docker version supports.
How the options compare
| Command | What it removes | Effect on next build |
|---|---|---|
| `docker builder prune` | Dangling cache only | Small |
| `docker builder prune -a` | All unused cache | Full rebuild |
| `docker builder prune -a --filter "until=168h"` | Unused cache older than 7 days | Only old steps rebuild |
| `docker builder prune --keep-storage 5GB` | Oldest cache beyond a 5 GB budget | Recent steps stay cached |
Common errors
- Expecting
docker builder pruneto free everything. Without-a, much of the cache stays. Checkdocker system dfagain and decide whether-ais acceptable. - Pruning right before a deadline build. A full prune makes the next build slow and can trigger large downloads. Prune after the build, not before it.
- Assuming
docker image pruneclears the cache. It does not. Images and build cache are separate objects, and each needs its own prune. - Pruning during a running build. Cache records in use by an active build are not removed, so the reclaimed space can be smaller than expected.
Exercise
A CI runner builds images all day. Its build cache is 40 GB, and you want to free space without making every build start from scratch. Which command fits, and why?
Show answer
Use docker builder prune -a --filter "until=168h", or docker builder prune --keep-storage 5GB. Both delete old unused cache but keep recent records, so current projects still get fast rebuilds. A plain docker builder prune -a would also work, but the next builds would all rerun every step.
Exercise
You ran docker builder prune and docker system df still shows a large reclaimable Build Cache. What did you probably miss?
Show answer
The command without -a removes only dangling cache. Run docker builder prune -a (optionally with --filter "until=168h") to remove all unused cache.