Skip to content

Instantly share code, notes, and snippets.

@tkihira
Created April 12, 2012 13:02
Show Gist options
  • Select an option

  • Save tkihira/2367067 to your computer and use it in GitHub Desktop.

Select an option

Save tkihira/2367067 to your computer and use it in GitHub Desktop.
rmdir recursively in node.js
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);
};
@dead-claudia
Copy link
Copy Markdown

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.

@aseigneurin
Copy link
Copy Markdown

Works like a charm. Thanks!

@camelaissani
Copy link
Copy Markdown

My implementation to remove recursively but without using recursive method. Just in case of deeper directory tree.

https://gist.github.com/camelaissani/ab4a9e6d69088d6f03a46ee2fd4fd112

@teratzul
Copy link
Copy Markdown

teratzul commented Sep 5, 2017

Works great! Thank you!!

@cancerberoSgx
Copy link
Copy Markdown

thanks! and thanks for the synchronous signature too! :P

@wulinjie122
Copy link
Copy Markdown

thanks, help me so much.

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