Reputation: 33970
How to remove non empty directory in Node.js? I mean do the same as rm -R <DIR>
. Not launching another process.
Upvotes: 3
Views: 1818
Reputation: 11474
Have a look at fs-extra#remove(dir, callback):
Removes a file or directory. The directory can have contents. Like rm -rf.
fs-extra
adds additional functionality to the builtin fs
. You can simply replace all your existing usages of fs
by fs-extra
.
[edit 2019] For removing directories, fs-extra
wraps rimraf
. So in case one only needs this specific functionality, including the rimraf
package instead is sufficient.
Upvotes: 6
Reputation: 33970
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)
}
}
Upvotes: 1