solvit.io

Delete a non-empty directory recursively with Node.js

Solution
var fs = require("fs");
var path = require("path");

var removeDir = function(dirPath) {
    if (fs.existsSync(dirPath)) {
        return;
    }

    var list = fs.readdirSync(dirPath);
    for (var i = 0; i < list.length; i++) {
        var filename = path.join(dirPath, list[i]);
        var stat = fs.statSync(filename);

        if (filename == "." || filename == "..") {
            // do nothing for current and parent dir
        } else if (stat.isDirectory()) {
            removeDir(filename);
        } else {
            fs.unlinkSync(filename);
        }
    }

    fs.rmdirSync(dirPath);
};
Example
var fs = require("fs");
var path = require("path");

var removeDir = function(dirPath) {
    // ...
};

removeDir('/tmp/output');
Find more solutions about: , ,