83

Is there an easy way to get rid of everything getting generated as a result of performing an SBT build? It turns out it creates target directories all over the place. Performing

sbt clean clean-cache clean-lib clean-plugins

... doesn't get rid of all.

Eugene Burmako
  • 12,892
  • 1
  • 41
  • 58
Wilfred Springer
  • 10,593
  • 4
  • 49
  • 69
  • I would like to see this too and have submitted a ticket: http://code.google.com/p/simple-build-tool/issues/detail?id=166 – Landon Kuhn May 16 '11 at 21:57
  • That issue tracker is no longer in use so I have resubmitted it here: https://github.com/sbt/sbt/issues/896 – Robin Green Oct 02 '13 at 17:44

4 Answers4

71

On my system (Ubuntu Linux) with SBT 0.13.5 and some projects from the Coursera Functional Programming course I found the folders all totalled up to 2.1GB for 12 projects due to all the cache files and duplicated Scala downloads.

The current SBT commands that work and get almost everything cleaned is:

sbt clean clean-files

This removes the top level "target" and "lib_managed" folders (23MB down to 3.2MB in this case) but leaves some target folders under project:

./project/project/project/target
./project/project/target
./project/target

This is where the Linux find command (also posted by @jack-oconnor) is very helpful:

find . -name target -type d -exec rm -rf {} \;

This gets us back down to a mere 444KB for one of my own projects and the 2.1GB goes down to 5.0MB !

In windows you won't have as many useful command line options, e.g. no star wildcards in path names, but you can always try and force it with:

rmdir /s /q target project/target project/project/target

The best I can do on automatically finding is a DIR command:

dir /ad /s /b | find "target"
RudeDude
  • 819
  • 6
  • 5
  • Is it necessary to have the `-f` flag when using the find command? Seems unnecessary and potentially unsafe. – unjankify Sep 19 '17 at 15:31
  • This worked fine for me: `find . -name target -type d -exec rm -r {} \;` – unjankify Sep 19 '17 at 15:38
  • The "-f" portion of the 'rm' command should not be needed but on some systems it might be preferable to a long list of deletion confirmations. – RudeDude Sep 20 '17 at 16:33
  • You may want to use `-prune` option in `find` to avoid `No such file or directory` message in terminal. So it is `find . -name target -type d -prune -exec rm -r {} \;`. See https://unix.stackexchange.com/q/115863 – Minh Thai Jan 31 '18 at 04:01
  • @RudeDude where do you read about `sbt clean clean-files`, I can't find its documentation anywhere – Minh Thai Jan 31 '18 at 04:04
  • @MinhThai This post is older and I was using SBT 0.13 but it still seems to be listed as an example in the 1.x docs online but not fully documented. When I checked on the command line just now I was able to run "sbt tasks" which lists all the different "commands" (called tasks) that you can run. The keyword "clean" is in this task list and says: "Deletes files produced by the build, such as generated sources, compiled classes, and task caches." – RudeDude Feb 01 '18 at 13:33
  • `sbt clean clean-files` not working with sbt v 1.1.1 – Albert Bikeev Mar 29 '19 at 10:10
  • 6
    @AlbertBikeev: maybe `sbt clean cleanFiles` works for you – serv-inc Jun 17 '19 at 07:53
  • 2
    sbt clean cleanFiles – frostcs Apr 24 '20 at 13:35
6

Obviously this is very important for reproducible builds on an integration server such as Jenkins!

Ensure that all files, including the ivy cache, are stored within the integration server workspace, by supplying command line arguments such as this to sbt:

-Dsbt.global.base=project/.sbtboot -Dsbt.boot.directory=project/.boot -Dsbt.ivy.home=project/.ivy

and then click the Wipe Out Workspace button in Jenkins, or the equivalent in other integration servers. That should definitely do it!

Or if you are using a recent version of the sbt launcher script, you can simply add -no-share instead.

Robin Green
  • 29,408
  • 13
  • 94
  • 178
  • If you're already using Jenkins, then the [cleanup plugin](https://stackoverflow.com/a/28728663/1080804) is a great alternative to making sure the workspace is cleared – ecoe Jan 20 '19 at 00:11
5

On Linux or similar, this is better than find -name, as it won't accidentally remove any directory named target that might exist in your source code:

find . -regextype posix-awk -regex \.(/project)*/target -exec rm -r {} +

If you're running this command within a shell, you'll need to quote the regular expression, for example, for bash:

find . -regextype posix-awk -regex '\.(/project)*/target' -exec rm -r {} +

With BSD find (e.g. on Mac OS X) the command will be:

find -E . -regex \.(/project)*/target -exec rm -r {} +
Adrian Hempel
  • 51
  • 1
  • 2
2

I agree with the very good suggested solutions, personally I include a slight variation as a gnu make task.

content of Makefile:

clean:
    find . -name target | xargs rm -fr

and then run:

make clean

I like using Makefiles as code as documentation.

Jose Miguel
  • 179
  • 2
  • 4