Remove directory which is not empty

sachin picture sachin · Aug 5, 2013 · Viewed 281.6k times · Source

In my Node application I need to remove a directory which has some files, but fs.rmdir only works on empty directories. How can I do this?

Answer

Morgan ARR Allen picture Morgan ARR Allen · Aug 5, 2013

There is a module for this called rimraf (https://npmjs.org/package/rimraf). It provides the same functionality as rm -Rf

Async usage:

var rimraf = require("rimraf");
rimraf("/some/directory", function () { console.log("done"); });

Sync usage:

rimraf.sync("/some/directory");