I have a SBT build where the tests create temporary files into a directory called temp. How can I tell SBT to delete this folder when I call the clean task?
-
3Shouldn't your tests clean up after themselves? – agilesteel May 06 '12 at 16:27
-
In fact, in normal case the test would clean up the temp files. But in this case I need the temp files to have a look into when sth fails. I simply want to add the temp directory to the directories the clean task deletes. – Felix May 07 '12 at 15:07
-
Which is the same semantics as `clean` @agilesteel - clean everything even if a previous step failed to do so or was stopped before getting to it. Isn't it? – matanster Oct 11 '15 at 09:42
4 Answers
Use this setting in the project containing the temp
directory:
cleanFiles <+= baseDirectory { base => base / "temp" }
This adds the "temp" directory to the list of files to recursively delete when clean
runs.
The <
means "configure in terms of other tasks/settings", the +
means append to the current list of files, and baseDirectory
is the setting providing the base directory of the project.
You can see how clean
is configured using the inspect
command, documented in more detail on the Inspecting Settings page. An edited sbt session shows usage in this case:
> inspect clean
Task: Unit
Description:
Deletes files produced by the build, such as generated sources, compiled classes, and task caches.
Dependencies:
clean-files
clean-keep-files
> inspect clean-files
Setting: scala.collection.Seq[java.io.File] = List(<project>/lib_managed, <project>/target, <project>/temp)
Description:
The files to recursively delete during a clean.
Dependencies:
managed-directory
target
base-directory
You can see this shows you if it is a task or setting, the type, a description, and the tasks/settings used as inputs.

- 6,999
- 1
- 26
- 31
-
Thank you for the detailed answer. Again I learned something more about SBT. Works like a charm now. – Felix May 10 '12 at 20:25
-
Mark Harrah's answer is now outdated.
Here is a version that works for sbt 0.13 and upwards, and is the only of the two versions that works in sbt 1.0 and onwards.
Updated key appending syntax.
Before sbt 0.13, you had the <+=
syntax for adding additional values, and you could apply
on keys.
With 0.13, a new, more uniform syntax was introduced, in part called value
DSL. As of 1.0, the old syntax has been removed.
// From the old, apply-based version...
cleanFiles <+= baseDirectory { base => base / "temp" }
// ...we change to .value and collection-like syntax
cleanFiles += baseDirectory.value / "temp"
cleanFiles
and other collection-based keys now mimic a (mutable) collection, so you can append values to it via a +=
operator. If you have multiple values, use ++=
with a List
or Seq
instead.
.value
does not force the evaluation of baseDirectory
when you write it, but each time cleanFiles
is calculated, so it's different for each sub-project.
Updated inspect and command syntax
There is also a slight difference in the inspect clean-files
syntax.
hyphen-named-commands
have been deprecated in 0.13 and removed in 1.0. They were replaced withlowerCamelCaseCommands
, so it's the same in the console as in yourbuild.sbt
.inspect
doesn't show the value of your key anymore (I could not find a version information for that). Instead, you have to useshow
.sbt:root-_t> show cleanFiles [info] * C:\Users\Adowrath\_T\target\scala-2.12 [info] * C:\Users\Adowrath\_T\target\streams [info] * C:\Users\Adowrath\_T\temp [success] Total time: 0 s, completed 06.02.2018, 10:28:00

- 701
- 11
- 24
One possibility is that your tests clean up after themselves, like agilesteel noted.
Another possiblity is that you create a custom cleanup task which depends on the test task. See my answer here for more information on how to customize existing tasks (like test in your case): add dependency to existing rule.
The previously suggested solution is deprecated now. Bellow is the code which works for me.
cleanFiles += new java.io.File(path)

- 123
- 1
- 5