var fs = require("fs"); | |
var path = require("path"); | |
var rmdir = function(dir) { | |
var list = fs.readdirSync(dir); | |
for(var i = 0; i < list.length; i++) { | |
var filename = path.join(dir, list[i]); | |
var stat = fs.statSync(filename); | |
if(filename == "." || filename == "..") { | |
// pass these files | |
} else if(stat.isDirectory()) { | |
// rmdir recursively | |
rmdir(filename); | |
} else { | |
// rm fiilename | |
fs.unlinkSync(filename); | |
} | |
} | |
fs.rmdirSync(dir); | |
}; |
This comment has been minimized.
This comment has been minimized.
silentroach
commented
Feb 15, 2013
why sync? |
This comment has been minimized.
This comment has been minimized.
metal3d
commented
Apr 11, 2013
@silentroach this is made to not be synchroneous. Sometimes (as for my use case) this is pretty nice to work synchroneously |
This comment has been minimized.
This comment has been minimized.
playerwtf
commented
Jun 9, 2013
You can safely remove the check for "." and ".." see documentation for nodejs "Synchronous readdir(3). Returns an array of filenames excluding '.' and '..'." |
This comment has been minimized.
This comment has been minimized.
azaslavsky
commented
May 13, 2014
Not sure if this is of any interest to anybody, but I made a fork of this gist that recursively deletes only empty directories. If a directory (or any of its descendant directories) has content inside it, it is left alone: |
This comment has been minimized.
This comment has been minimized.
ajwinn
commented
Aug 30, 2014
Thanks! |
This comment has been minimized.
This comment has been minimized.
meteormatt
commented
Oct 16, 2014
Thanks! |
This comment has been minimized.
This comment has been minimized.
isiahmeadows
commented
Dec 27, 2014
This indirectly inspired me to implement the same functionality, both synchronously and asynchronously, in LiveScript. The async one uses Promises underneath, but its global API is nodeback-based. |
This comment has been minimized.
This comment has been minimized.
aseigneurin
commented
Oct 9, 2015
Works like a charm. Thanks! |
This comment has been minimized.
This comment has been minimized.
camelaissani
commented
Apr 17, 2016
My implementation to remove recursively but without using recursive method. Just in case of deeper directory tree. https://gist.github.com/camelaissani/ab4a9e6d69088d6f03a46ee2fd4fd112 |
This comment has been minimized.
This comment has been minimized.
teratzul
commented
Sep 5, 2017
Works great! Thank you!! |
This comment has been minimized.
This comment has been minimized.
cancerberoSgx
commented
May 30, 2019
thanks! and thanks for the synchronous signature too! :P |
This comment has been minimized.
skovalyov commentedJan 23, 2013
Thank you for this Gist. Just made a CoffeeScript clone of it at https://gist.github.com/4605986