Instantly share code, notes, and snippets.

Embed
What would you like to do?
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);
}
};
@elpeleq42

This comment has been minimized.

elpeleq42 commented Jul 2, 2018

doesn't work. Says that the directory is not empty

@elpeleq42

This comment has been minimized.

elpeleq42 commented Jul 2, 2018

Also this code would be a minified version of yours:

function rmdir(d) {
    var self = arguments.callee
    if (fs.existsSync(d)) {
        fs.readdirSync(d).forEach(function(file) {
            var C = d + '/' + file
            if (fs.statSync(C).isDirectory()) self(C)
            else fs.unlinkSync(C)
        })
        fs.rmdirSync(d)
    }
}

If you could fix that(or yours) it would help me a lot

@laazebislam

This comment has been minimized.

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