Skip to content

Instantly share code, notes, and snippets.

@UziTech UziTech/rimraf.js
Created Dec 9, 2016

Embed
What would you like to do?
Remove a file or directory recursively
const fs = require("fs");
const path = require("path");
function rimraf(dir) {
return new Promise(function (resolve, reject) {
fs.lstat(dir, function (err, stats) {
if (err) return reject(err);
if (stats.isDirectory()) {
fs.readdir(dir, function (err, files) {
if (err) return reject(err);
Promise.all(files.map(function (file) {
return rimraf(path.join(dir, file));
})).then(function () {
fs.rmdir(dir, function (err) {
if (err) return reject(err);
resolve();
});
}).catch(reject);
});
} else {
fs.unlink(dir, function (err) {
if (err) return reject(err);
resolve();
});
}
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.