Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is node.js rmdir recursive ? Will it work on non empty directories?

Tags:

node.js

The documentation for fs.rmdir is very short and doesn't explain the behavior of rmdir when the directory is not empty.

Q: What happens if I try to use this API to delete a non empty directory ?

like image 995
Samuel Rossille Avatar asked Sep 27 '12 18:09

Samuel Rossille


People also ask

Can you remove a non empty directory using rmdir?

rmdir is a command-line utility for deleting empty directories, while with rm you can remove directories and their contents recursively. If a directory or a file within the directory is write-protected, you will be prompted to confirm the deletion.

Which command can be used to remove a non empty directory recursively?

To remove a directory and all its contents, including any subdirectories and files, use the rm command with the recursive option, -r . Directories that are removed with the rmdir command cannot be recovered, nor can directories and their contents removed with the rm -r command.

How would you remove a directory that is not empty?

To remove a directory that is not empty, use the rm command with the -r option for recursive deletion. Be very careful with this command, because using the rm -r command will delete not only everything in the named directory, but also everything in its subdirectories.

Which method of FS module is used to remove a directory?

The fs. rmdir() method is used to delete a directory at the given path.


2 Answers

Although using a third-party library for such a thing I could not come up with a more elegant solution. So I ended up using the npm-module rimraf.

Install it

npm install rimraf 

Or install it and save to 'package.json' (other save options can be found in the npm-install docs)

npm install --save rimraf 

Then you can do the following:

rmdir = require('rimraf'); rmdir('some/directory/with/files', function(error){}); 

Or in Coffeescript:

rmdir = require 'rimraf' rmdir 'some/directory/with/files', (error)-> 
like image 195
Besi Avatar answered Sep 29 '22 13:09

Besi


I wrote about this problem exactly.

My previous solution below, while simple, is not preferred. The following function, is a Synchronous solution; while async might be preferred.

deleteFolderRecursive = function(path) {     var files = [];     if( fs.existsSync(path) ) {         files = fs.readdirSync(path);         files.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);     } }; 

[Edit] Added lstat instead of stat to prevent errors on symlinks

[Previous Solution]

My solution to this is quite easy to implement.

var exec = require('child_process').exec,child; child = exec('rm -rf test',function(err,out) {    console.log(out); err && console.log(err);  }); 

This is slimmed down for this page, but the basic idea is simple; execute 'rm -r' on the command line. If your app needs to run across different types of OS, put this in a function and have an if/else/switch to handle it.

You will want to handle all the responses; but the idea is simple enough.

like image 35
geedew Avatar answered Sep 29 '22 13:09

geedew