3.6.4 Disk Cache in CI

--disk_cache is the cheapest way to make a cold CI runner less wasteful: persist one directory across runs, and Bazel can reuse action results even though the server process itself starts empty every time.1,2 It works best when the same machine or restored volume survives between jobs. Once many stateless workers need to share the same outputs, you are already leaving the "local disk" world and moving toward 3.6.5 Simple Remote Cache Setup.1,3

What Bazel stores in the disk cache

The key mental model from 2.4 Caching & Incrementality still applies here: Bazel is not caching "the build succeeded", it is caching concrete action results. The disk cache uses the same two-store model as remote cache infrastructure: an Action Cache (ac/) keyed by the action digest, and a Content Addressable Store (cas/) keyed by content hash.2 On a hit, Bazel can reconstruct the outputs from that cache instead of re-running the action.

That is why --disk_cache helps even when CI jobs are separate Bazel invocations. The in-memory server state from 3.6.2 Bazel Server Lifecycle in CI is gone, but the external cache directory is still there, so successful actions can be reused across jobs, after bazel shutdown, and even after bazel clean.2,3

When disk cache is enough

Disk cache is the right first step after 3.6.1 Basic CI Recipe when reuse happens on one runner or through the CI platform's directory restore/save mechanism.3 The line is clear: stateful workers benefit from local repository and disk caches on their own filesystem, while stateless workers lose those local caches when the machine is replaced and therefore need a shared remote cache instead.3

That boundary matters because --disk_cache is still local state. It reduces repeated execution work, but it does not solve cross-runner sharing. If the same commit lands on different workers and you expect all of them to see the same cached outputs, go to 3.6.5 Simple Remote Cache Setup rather than trying to treat one machine's disk as team-wide infrastructure.1,3

Put the path behind a named CI config

This is a 3.2 Project Configuration concern, not something every CI step should spell out inline. The core flag is:

build:ci --disk_cache=/path/to/build/cache

The important part is not the exact directory name. The important part is that the path is stable and points to a location your CI system actually persists between runs.1 For developer machines, ~ can be used in the path and is expanded by Bazel, which makes a checked-in .bazelrc practical when the same cache pattern should apply outside CI as well.1 The ci-cache-baseline .bazelrc wires the flag with an explicit path, build:ci --disk_cache=~/.cache/bazel-disk, alongside a --repository_cache line on the same build:ci block.

Which value forms your Bazel version accepts

Use an explicit --disk_cache=<path> in shared presets. It is the one enabling form that means the same thing in every version. The shorthand forms changed meaning in Bazel 10, and on older versions they fail in surprising ways:4

FormBazel 9.x and olderBazel 10
--disk_cache=<path>Cache at <path>Cache at <path>
--disk_cache= (empty)DisabledDisabled
--disk_cache (no value)Takes the next argument as the path. bazel build --disk_cache //:app treats //:app as the directory and builds nothingCache at <outputUserRoot>/cache/disk
--disk_cache=trueA cache directory literally named trueCache at <outputUserRoot>/cache/disk
--disk_cache=falseA cache directory literally named false, so the cache is still onDisabled
--nodisk_cacheError: Illegal use of 'no' prefix on non-boolean optionDisabled

The dangerous row is --disk_cache=false on 9.x. The command succeeds, the cache keeps working, and a stray false/ directory with ac/ and cas/ appears relative to the workspace. The official documentation describes the Bazel 10 forms, so check which version a doc page or snippet assumes before copying a disabling line. --disk_cache= disables the cache in both versions.

think

Trace: A CI preset imports a shared .bazelrc that sets build --disk_cache=/cache/bazel-disk. A particular job tries to "clear" it by appending build:ci --disk_cache=false. The repository pins Bazel 9.3.1. Did that disable disk cache?

Reveal

No. The later value does override the inherited path, because .bazelrc lines accumulate and the last value for a flag wins.1 But on 9.x false is just a relative path, so the job now writes a fresh cache into a directory named false. It loses the shared cache and still pays for a disk cache. On Bazel 10 the same line disables the cache.4

Use build:ci --disk_cache=, which disables the cache in both versions. The general lesson holds for any rc layering: a later layer can silently undo the shared cache preset. Inspect --announce_rc output to see which line won.

Growth is the operational catch

The biggest downside of disk cache in CI is not correctness, but growth. Every newly cached action adds more files, so a long-lived runner can eventually trade faster builds for a full disk if nothing bounds the cache.1 Built-in garbage-collection flags address this. Bazel runs the cleanup while idle between builds. The flags cap the cache by size and by entry age. Their names depend on the version:1,4

Bazel versionFlag names
7.4 – 9.x--experimental_disk_cache_gc_max_size, --experimental_disk_cache_gc_max_age, --experimental_disk_cache_gc_idle_delay
10--disk_cache_gc_max_size, --disk_cache_gc_max_age, --disk_cache_gc_idle_delay

Bazel 10 still accepts the experimental_ spellings as old names, so a 9.x .bazelrc keeps working after the upgrade. The reverse is not true: a 9.x Bazel rejects the new names as unknown options. Keep the experimental_ names until every consumer of the rc file runs Bazel 10.

If you are not ready to rely on those flags, the fallback is operational: clean the cache directory explicitly or move to a cache service that owns retention for you.1,3 That is one of the natural transition points from local disk cache to 3.6.5 Simple Remote Cache Setup: not just sharing across machines, but also wanting cache lifecycle management that is no longer "some directory on a runner".

extra

When --disk_cache and --remote_cache are both set

Once both flags are enabled, Bazel uses a combined cache rather than treating them as two unrelated features.5 The surprising part is worth spelling out: the pair behaves like remote cache overall, unless --incompatible_remote_results_ignore_disk is set, in which case the disk cache keeps acting like a local cache even when remote accept/upload flags are turned off.5

That is a real configuration edge, but it is already "simple remote cache" territory, not the core of this article. If your normal CI config mixes both layers, finish this chapter for the local-disk mental model, then continue with 3.6.5 Simple Remote Cache Setup.

key takeaway

--disk_cache is the first CI cache worth adding because it needs no extra service: just one persistent directory and a stable build:ci config. It shines on one runner or on CI platforms that restore the same directory between jobs. Once cache policy becomes about branch strategy, periodic clean runs, and resilience under failure, the next layers are 3.6.6 Cache Warm/Cold Considerations and 6.8 Build Service Operations.

Check your understanding · 3 questions

1.A CI platform restores a disk cache directory before each job and saves it afterward. The runner is ephemeral (destroyed after each job). What does --disk_cache provide in this setup?

Select one answer

2.True or false about disk cache behavior and management:

Choose True or False for each sentence

The disk cache survives bazel clean and even server restarts, because it lives outside the output base.
On Bazel 9.x, appending --disk_cache=false to a later rc layer turns the disk cache off.
Shared CI presets should prefer an explicit --disk_cache=<path> so cache location and persistence are obvious.
When both --disk_cache and --remote_cache are set, the disk cache is always checked first and the remote cache is ignored on hits.

3.When is a local disk cache the right choice vs. a remote cache?

Select one answer

0 of 3 answered

Footnotes

  1. Remote Caching and Command-Line Reference — --disk_cache as an opt-in filesystem-backed external cache, checked-in .bazelrc usage with the ~ alias, boolean-style enable/disable forms, and disk-cache GC flags ↩1 ↩2 ↩3 ↩4 ↩5 ↩6 ↩7 ↩8 ↩9

  2. The Many Caches of Bazel — disk cache as local AC/CAS, with visible ac/ and cas/ directories and action-digest-based reuse ↩1 ↩2 ↩3

  3. How Bazel built its CI system on top of Buildkite — stateful workers benefiting from local disk cache, stateless workers needing remote cache, and reuse surviving separate jobs ↩1 ↩2 ↩3 ↩4 ↩5 ↩6

  4. Bazel 10.0.0rc1 release notes — --disk_cache without an argument, the boolean enable and disable forms, and the disk-cache GC flags leaving experimental status. The 9.x column of the table was verified by running each form on 9.3.1 and 10.0.0rc2 ↩1 ↩2 ↩3

  5. Bazel's Remote Caching and Remote Execution Explained — combined-cache behavior when --disk_cache and --remote_cache are enabled together ↩1 ↩2