Skip to content

Instantly share code, notes, and snippets.

@hughsk
Created February 1, 2012 07:47
Show Gist options
  • Save hughsk/1715822 to your computer and use it in GitHub Desktop.
Save hughsk/1715822 to your computer and use it in GitHub Desktop.
Synchronous `rmdir --recursive` for NodeJS.
// Thanks to [liangzan's gist](https://gist.github.com/807712)
rmdirSyncForce = function(path) {
var files, file, fileStats, i, filesLength;
if (path[path.length - 1] !== '/') {
path = path + '/';
}
files = fs.readdirSync(path);
filesLength = files.length;
if (filesLength) {
for (i = 0; i < filesLength; i += 1) {
file = files[i];
fileStats = fs.statSync(path + file);
if (fileStats.isFile()) {
fs.unlinkSync(path + file);
}
if (fileStats.isDirectory()) {
rmdirSyncForce(path + file);
}
}
}
fs.rmdirSync(path);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment