Skip to content

Commit a4a46e9

Browse files
committed
add ignoreErrors to os.remove.all
1 parent 90626df commit a4a46e9

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

Readme.adoc

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1028,7 +1028,7 @@ If you wish to remove the destination of a symlink, use
10281028

10291029
[source,scala]
10301030
----
1031-
os.remove.all(target: Path): Unit
1031+
os.remove.all(target: Path, ignoreErrors: Boolean = false): Unit
10321032
----
10331033

10341034
Remove the target file or folder; if it is a folder and not empty, recursively
@@ -1063,6 +1063,12 @@ os.exists(wd / "misc/broken-symlink", followLinks = false) ==> false
10631063
If you wish to remove the destination of a symlink, use
10641064
<<os-readlink>>.
10651065

1066+
``os.remove.all`` removes nested files and folders one at a time, and any failure
1067+
in removing a file (e.g. due to permissions) or folder (e.g. due to someone concurrently
1068+
creating a file within it) causes an error to be thrown and terminates the removal early.
1069+
You can pass `ignoreErrors = false` to continue with the deletion of other files
1070+
even if some files or folders failed to be removed.
1071+
10661072
==== `os.hardlink`
10671073

10681074
[source,scala]

os/src/FileOps.scala

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -329,16 +329,21 @@ object remove extends Function1[Path, Boolean] {
329329
}
330330

331331
object all extends Function1[Path, Unit] {
332-
def apply(target: Path) = {
332+
def apply(target: Path): Unit = apply(target, ignoreErrors = false)
333+
def apply(target: Path, ignoreErrors: Boolean = false): Unit = {
333334
require(target.segmentCount != 0, s"Cannot remove a root directory: $target")
334335
checker.value.onWrite(target)
335336

336337
val nioTarget = target.wrapped
337338
if (Files.exists(nioTarget, LinkOption.NOFOLLOW_LINKS)) {
338339
if (Files.isDirectory(nioTarget, LinkOption.NOFOLLOW_LINKS)) {
339-
walk.stream(target, preOrder = false).foreach(remove(_))
340+
for (p <- walk.stream(target, preOrder = false)) {
341+
try remove(p)
342+
catch { case e: Throwable if ignoreErrors => /*ignore*/ }
343+
}
340344
}
341-
Files.delete(nioTarget)
345+
try Files.delete(nioTarget)
346+
catch { case e: Throwable if ignoreErrors => /*ignore*/ }
342347
}
343348
}
344349
}

0 commit comments

Comments
 (0)