An easy way to get rid of *everything* generated by SBT?

ScalaSbt

Scala Problem Overview


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.

Scala Solutions


Solution 1 - Scala

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"

Solution 2 - Scala

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.

Solution 3 - Scala

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 {} +

Solution 4 - Scala

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.

Solution 5 - Scala

You can use Pretty Clean to clean the all of dev tools caches including SBT.

PrettyClean also cleans the SBT project's target folder.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionWilfred SpringerView Question on Stackoverflow
Solution 1 - ScalaRudeDudeView Answer on Stackoverflow
Solution 2 - ScalaRobin GreenView Answer on Stackoverflow
Solution 3 - ScalaAdrian HempelView Answer on Stackoverflow
Solution 4 - ScalaJose MiguelView Answer on Stackoverflow
Solution 5 - ScalaAndy AiView Answer on Stackoverflow