Skip to content

Instantly share code, notes, and snippets.

@borkdude
Last active Oct 1, 2020
Embed
What would you like to do?
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