Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clean up unreachable generated files in Bazel

Tags:

bazel

Suppose I have a very minimal project with an empty WORKSPACE and a single package defined at the project root that simply uses touch to create a file called a, as follows:

genrule(
  name = "target",
  cmd = "touch $@",
  outs = ["a"],
)

If I now run

bazel build //:target

the package will be "built" and the a file will be available under bazel-genfiles.

Suppose I now change the BUILD to write the output to a different file, as follows:

genrule(
  name = "target",
  cmd = "touch $@",
  outs = ["b"],
)

Building the same target will result in the file b being available under bazel-genfiles. a will still be there though, even though at this point it's "unreachable" from within the context of the build definition.

Is there a way to ask Bazel to perform some sort of "garbage collection" and remove files (and possibly other content) generated by previous builds that are no longer reachable as-per the current build definition, without getting rid of the entire directory? The bazel clean command seems to adopt the latter behavior.

There seems to be a feature in the works, but apparently it cannot be performed on demand, but rather it executes automatically as soon as a certain threshold has been reached.

like image 920
stefanobaghino Avatar asked Sep 21 '25 03:09

stefanobaghino


1 Answers

Note that running bazel clean will not actually delete the external directory. To remove all external artifacts, use bazel clean --expunge

like image 75
Milind Deore Avatar answered Sep 23 '25 10:09

Milind Deore