Skip to content

Instantly share code, notes, and snippets.

@royswale
Forked from geedew/node-rm-rf.js
Created February 3, 2021 12:36
Show Gist options
  • Select an option

  • Save royswale/e34deb837cb15ced51c71f5cbe7ed9dc to your computer and use it in GitHub Desktop.

Select an option

Save royswale/e34deb837cb15ced51c71f5cbe7ed9dc to your computer and use it in GitHub Desktop.
Removing a directory that is not empty in NodeJS
var fs = require('fs');
var deleteFolderRecursive = function(path) {
if( fs.existsSync(path) ) {
fs.readdirSync(path).forEach(function(file,index){
var curPath = path + "/" + file;
if(fs.lstatSync(curPath).isDirectory()) { // recurse
deleteFolderRecursive(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment