ClearDisk
Buy for $10Download ClearDiskDownload
A LITTLE MORE CLARITY

How to clear the Docker build cache on Mac

Run docker builder prune to remove build cache nothing is using, add --filter "until=24h" to keep what recent builds used, or -a to remove more. Check first with docker buildx du or docker system df, because every layer you delete has to be rebuilt or downloaded on the next build.

1. Measure the build cache

docker system df has a Build Cache row with a total and a reclaimable figure. For detail, docker buildx du lists each cache record for the selected builder with its size, when it was last used and whether it is reclaimable. A record marked not reclaimable is in use by the builder and won’t be deleted even with -a.

An asterisk after a size means the record shares storage with an image. Pruning it removes cache metadata, but the image still needs those layers, so little space comes back. In Docker Desktop, Settings → Builders shows each builder; inspecting an active builder includes its disk usage.

docker system df
docker buildx du
docker buildx du --verbose

2. Prune unused build cache

docker builder prune asks for confirmation, then deletes cache that nothing is using and reports how much it reclaimed. With Docker Desktop, the command runs through Buildx: docker builder prune --help shows docker buildx prune as its usage, and it acts on the selected builder, normally desktop-linux. Add --builder with a name to prune a different builder. Records that a running build is using count as in use and stay, so prune between builds.

Builders that use the docker-container driver keep their cache in their own container. Removing such a builder in Settings → Builders removes its cache along with it; Docker Desktop won’t remove the builder that is currently selected.

docker builder prune
docker builder prune --builder mybuilder

3. Keep recent cache, or remove more

A filter keeps the cache you are actively using. --filter "until=24h" keeps records used in the last 24 hours and removes older ones, so today’s project still builds quickly. Size flags trim least-recently-used records until the cache fits a limit: current Buildx calls it --max-used-space, and older references list --keep-storage. docker builder prune --help shows which one your version accepts.

-a widens the prune. Docker’s builder prune reference describes it as removing all unused build cache rather than only dangling records, and the Buildx help describes it as including internal and frontend images. Either way the next build will download base images again and rerun every step. Cache mounts, the RUN --mount=type=cache folders package managers use, are build cache too.

docker builder prune --filter "until=24h"
docker builder prune --max-used-space 10gb
docker builder prune -a

Let garbage collection keep it in check

BuildKit already clears old cache on a schedule. Docker’s documentation lists the defaults: easily regenerated cache unused for 48 hours goes first, then anything unused for 60 days, then whatever exceeds the size limit. With Docker Desktop that limit is builder.gc.defaultKeepStorage in Settings → Docker Engine, which is 20GB by default.

If build cache keeps returning to a size you can’t spare, lower that value in the JSON on the Docker Engine page and apply the change, rather than pruning by hand every week. Keep the JSON valid and change only that value. Builders you create yourself with docker buildx create don’t read this setting; Docker’s documentation configures their garbage collection in a BuildKit configuration file instead.

Build cache and docker system prune

docker system prune always includes unused build cache, cache mounts included, alongside stopped containers, unused networks and dangling images. If you run it to tidy containers, expect a slower next build. When cache is the only thing you want gone, docker builder prune is the precise tool. The reverse also holds: deleting images doesn’t empty the build cache, and docker system df counts the two separately.

After pruning, Docker Desktop returns the freed blocks to macOS through its disk image. The Docker.raw guide shows how to confirm that with du rather than the file’s listed size.

Common questions

What's the difference between docker builder prune and docker system prune for build cache?

docker builder prune is the precise tool when only build cache is the target. docker system prune also removes stopped containers, unused networks, and dangling images along with the cache, so it will slow your next build more than needed.

Will clearing Docker's build cache make my next build slower?

Yes. Removing cache means the next build has to download base images again and rerun every step, so it's worth checking usage with docker buildx du first, since some records aren't reclaimable while a builder is actively using them.

Does Docker clean up old build cache on its own?

Yes, BuildKit clears old cache on a schedule: easily regenerated cache unused for 48 hours goes first, then anything unused for 60 days, then whatever exceeds the size limit, which is 20GB by default in Docker Desktop.

Does clearing Docker's build cache also remove unused images?

No. Deleting images doesn't empty the build cache, and docker system df counts the two separately, so clearing one doesn't reduce the other.

References & further reading

These guides combine the linked official guidance with ClearDisk’s documented behavior. Your files and app settings determine the right next step.

Main guide: developer storage on Mac · Related pillar: clear Xcode Derived Data on Mac · Browse all Developer storage guides →

FROM A NUMBER TO AN ANSWER

See what’s using
your space.

Explore your Mac with ClearDisk’s free local scanner.
Review the files. Keep the control.

Download free scannermacOS 15+ · version 2.2.1Cleanup license: $10 once