Infrastructure
Build Cache Disk Growth: Set a Safe BuildKit Budget
Identify the active builder and cache scope before cleanup, then balance free disk space, rebuild time and retention with a tested BuildKit cleanup policy.
In this article
Build cache growth should be managed as a bounded resource, not solved by deleting arbitrary directories until a build works again. Identify which builder owns the cache, separate it from persistent application data, and choose a retention policy that fits both disk capacity and rebuild cost.
Docker's BuildKit garbage-collection documentation distinguishes immediate pruning from periodic policy-driven cleanup. The important operational question is not which command removes the most bytes, but which cache records can be discarded safely in the environment you actually use.
Confirm where the space is consumed
Start with filesystem-level usage and the builder's own inventory. Containers, images, build caches, logs, and application volumes can occupy different locations. A large Docker-related directory is not proof that build cache is the entire problem.
Record the active builder, driver, and host. A local command can target a remote or separate builder depending on configuration. Confirm the target before interpreting its usage or planning a cleanup window. Names remembered from another workstation are not sufficient evidence.
Compare free bytes with the workload's temporary space requirement. A build may need room for downloaded dependencies and intermediate layers before old cache is reclaimed. An apparently generous percentage of free space can still be too small for one unusually large build.
Separate replaceable cache from unique data
Classify what can be regenerated from reviewed source and available dependencies. Build cache is generally intended to accelerate repeated work, but a team can still rely operationally on an unavailable upstream artifact or a poorly documented local input.
Before removing anything, test whether a clean build can obtain its inputs through the approved path. If a supposedly disposable cache is the only remaining copy of an essential artifact, fix the artifact-retention problem first. Do not turn that discovery into an excuse to keep every cache forever.
Keep application volumes, database storage, and uploaded files outside the cleanup target. A broad system-prune instruction can affect more than the build cache the incident is about. Resolve explicit targets and understand the selected command's behavior before authorizing it.
Measure the cost of a cold build
Run a clean or appropriately isolated build in a safe environment and record duration, network use, and dependency availability. Compare that with a warm build using the same source revision. This establishes what cache retention is buying rather than assuming all old entries are equally valuable.
For an invented example, a warm build takes four minutes and a cold build takes fourteen. If aggressive cleanup causes six cold builds each day, the additional hour of build time may matter more than preserving a few extra gigabytes. These figures illustrate a tradeoff, not a forecast for your project.
Include failure behavior. A cold build that contacts an unreliable dependency service has a different operational risk from one that reads a durable internal mirror. Cache policy and dependency availability should be considered together.
Define a budget with working headroom
Choose a cache budget that leaves room for the operating system, active builds, and other known consumers. Avoid making the cache target equal to every currently unused byte. The next build needs space to do useful work before it can produce a smaller final image.
Document the retention intent: preserve recently useful records, remove older replaceable records first, and monitor whether the policy actually frees enough space. Review the current BuildKit version's supported policy fields rather than pasting settings from a different driver.
Keep policy order visible in review. A broad rule evaluated too early can defeat a narrower retention choice. Compare the intended and deployed configuration with JSON Diff when the daemon uses a JSON configuration, after removing secrets and unrelated private fields.
Plan cleanup as an explicit change
Pause or coordinate with active builds when the environment requires it. Record the exact builder and cleanup scope, expected effect, and recovery approach. A cache deletion may be recoverable by rebuilding, but the lost time and network activity are still real consequences.
Do not promise a dry-run mode unless the deployed command actually supports one. Read-only inventory is useful preparation, but it is not automatically an exact simulation of what a later prune operation will remove. Preserve that distinction in the change request.
Apply a bounded policy to a test builder first where practical. Check both reclaimed space and subsequent build behavior. A successful cleanup exit code is not evidence that the retained cache matches the team's performance expectations.
Observe the policy over several build cycles
Track free disk, cache usage, cold-build frequency, and build completion time. Alert before remaining space falls below the largest plausible build's working requirement. A storage alert that fires only after writes fail arrives too late for routine maintenance.
Revisit the budget when repositories, architectures, or build concurrency change. A multi-platform build can create a different cache shape from the single-platform workload used in the original estimate.
Conclusion
Safe build-cache management starts with scope and reproducibility. Inventory the correct builder, protect unique data, measure cold-build cost, and test a retention policy with working headroom. Optimize predictable builds, not just a one-time free-space number.