At this point, I feel like every company using Bazel with Git worktrees has rolled its own solution to the ever-growing disk usage caused by accumulating Bazel output bases.
Since Bazel creates a separate output base for each workspace, creating and deleting Git worktrees can leave behind quite a bit of unused build data.
Fortunately, Bazel 10 introduces a new startup flag that makes cleaning up these output bases much easier.
–delete_output_base_on_shutdown
To cut to the chase, simply add the following to your .bazelrc:
startup --delete_output_base_on_shutdown
With this flag enabled, Bazel automatically deletes the output base when its server shuts down.
You can see how it works in Bazel 10 pull request.
I wouldn’t recommend enabling this unconditionally in your main .bazelrc. Deleting the output base also means losing the incremental build state, which can make subsequent builds significantly slower.
Instead, I’d recommend detecting whether you’re running inside a Git worktree from your tools/bazel wrapper and enabling the flag only in that case.
You can pass the startup flag directly to Bazel or include it in a .bazelrc generated by your wrapper.
One caveat: the flag is supported only on local filesystems, not network filesystems such as NFS.
Conclusion
A very welcome addition to Bazel 10 that simplifies managing output bases when working with Git worktrees. Hopefully, this means you can finally get rid of that homegrown cleanup solution.