Skip to content

Commit

Permalink
add ignoreErrors to os.remove.all
Browse files Browse the repository at this point in the history
  • Loading branch information
lihaoyi committed Jan 8, 2025
1 parent 90626df commit a4a46e9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
8 changes: 7 additions & 1 deletion Readme.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1028,7 +1028,7 @@ If you wish to remove the destination of a symlink, use

[source,scala]
----
os.remove.all(target: Path): Unit
os.remove.all(target: Path, ignoreErrors: Boolean = false): Unit
----

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

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

==== `os.hardlink`

[source,scala]
Expand Down
11 changes: 8 additions & 3 deletions os/src/FileOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -329,16 +329,21 @@ object remove extends Function1[Path, Boolean] {
}

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

val nioTarget = target.wrapped
if (Files.exists(nioTarget, LinkOption.NOFOLLOW_LINKS)) {
if (Files.isDirectory(nioTarget, LinkOption.NOFOLLOW_LINKS)) {
walk.stream(target, preOrder = false).foreach(remove(_))
for (p <- walk.stream(target, preOrder = false)) {
try remove(p)
catch { case e: Throwable if ignoreErrors => /*ignore*/ }
}
}
Files.delete(nioTarget)
try Files.delete(nioTarget)
catch { case e: Throwable if ignoreErrors => /*ignore*/ }
}
}
}
Expand Down

0 comments on commit a4a46e9

Please sign in to comment.