Skip to content

Instantly share code, notes, and snippets.

@borkdude
Last active October 1, 2020 12:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save borkdude/2a85971164a7e71282d0d0a59fb4b60f to your computer and use it in GitHub Desktop.
Save borkdude/2a85971164a7e71282d0d0a59fb4b60f to your computer and use it in GitHub Desktop.
Delete file tree recursively using Java NIO
#!/usr/bin/env bb
;; requires bb 0.2.2 or higher
(require '[clojure.java.io :as io])
(defn delete-recursively [f]
(let [dir (io/file f)]
(if (.exists dir)
(do (java.nio.file.Files/walkFileTree
(.toPath dir)
(reify java.nio.file.FileVisitor
(preVisitDirectory [this p attrs]
java.nio.file.FileVisitResult/CONTINUE)
(postVisitDirectory [this p attrs]
(.delete (io/file (.toFile ^java.nio.file.Path p)))
java.nio.file.FileVisitResult/CONTINUE)
(visitFile [this p attrs]
(.delete (io/file (.toFile ^java.nio.file.Path p)))
java.nio.file.FileVisitResult/CONTINUE)))
true)
false)))
(delete-recursively (first *command-line-args*))
;; $ mkdir -p /tmp/foo/bar/baz
;; $ delete.clj /tmp/foo
;; true
;; $ delete.clj /tmp/foo
;; false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment