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?
Node.js – Remove directory which is not empty
filesystemsnode.js
Related Question
- Javascript – How to get a list of the names of all files present in a directory in Node.js
- Node.js – Check synchronously if file/directory exists in Node.js
- Node.js + Nginx – What now
- Javascript – How to decide when to use Node.js
- Linux – How to recursively find and list the latest modified files in a directory with subdirectories and times
- Node.js – How to update NodeJS and NPM to the next versions
- Javascript – Using Node.JS, how to read a JSON file into (server) memory
- Javascript – How to completely uninstall Node.js, and reinstall from beginning (Mac OS X)
Best Solution
As of 2019...
As of Node.js 12.10.0,
fs.rmdirSync
supports arecursive
options, so you can finally do:Where the
recursive
option deletes the entire directory recursively.Update: The
recursive
option infs.rmdir
/fs.rmdirSync
has been deprecated however, so usefs.rm
/fs.rmSync
instead:The
force: true
option ignores exceptions ifdir
does not exist. More info see thefs.rmSync
docs