Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
To delete a directory recursively in Clojure.
(defn delete-recursively [fname]
(let [func (fn [func f]
(when (.isDirectory f)
(doseq [f2 (.listFiles f)]
(func func f2)))
( f))]
(func func ( fname))))

artekcs commented Apr 8, 2015

Fantastic. This is very useful on Windows when you can't delete insane directory hierarchies because of the MAX_PATH limitation. Just fire a "lein repl", load your script and (delete-recursively "insane/path").
Thanks a lot


erdos commented Jan 3, 2017 edited

this does the same using the build-in file-seq function to enumerate the contents of the directory.

(defn delete-recursively [fname]
  (doseq [f (reverse (file-seq ( fname)))]
     ( f)))

ulsa commented May 18, 2017

It's elegant, but I'm not sure it's as robust for large trees as the recursive version. reverse removes the laziness of file-seq.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment