vic-3
vic-3

Reputation: 994

delete folder containing files node js

I am looking for a way to delete folders that contain files in node.js ?

I know there exists a way to delete empty folders using fs.rmdir(), and i tried using the npm rimraf module that provides the function "rm -rf" for node.js

socket.on("end", function (data) {
    rimraf("./a/b/c", function(err){
      if(err){
        console.log(err);
      }
    });
  });

but i keep getting this error.

{ [Error: ENOTEMPTY: directory not empty, rmdir './a/b/c']
  errno: -39,
  code: 'ENOTEMPTY',
  syscall: 'rmdir',
  path: './a/b/c' }

So I tried another way around this issue, first i empty the directory then i delete the directory

   socket.on("end", function (data) {
        rimraf("./a/b/c/*", function(err){
          if(err){
            console.log(err);
          }else{
              fs.rmdir("./a/b/c")
           }
        });
      });

but then i get this error

Error: ENOTEMPTY: directory not empty, rmdir './a/b/c'
    at Error (native)

I checked the folders the rimraf deletes the files but i don't see why i am still getting an error with fs.rmdir().

Edit : I looked up a different module called fs-extra and came up with this.

fse.emptyDir("a/b/c/", function(err){
  if(err){
    console.log(err);
  } else {
    console.log("doneaaaa")
    fse.remove("a/b/c",function(err){
      if(err){
        console.log(err);
      } else {
        console.log('doneaswell');
      }
    });
  }
});

Now i get this error :

doneaaaa
{ [Error: EBUSY: resource busy or locked, unlink 'a/b/c/.nfs000000002ab5000d00000072']
  errno: -16,
  code: 'EBUSY',
  syscall: 'unlink',
  path: 'a/b/c/.nfs000000002ab5000d00000072' }

As you can see i get the past the first part of the function that deletes the files from the folder but when it comes to deleting the folder it throws the EBUSY error.

Thank you in advance !

Upvotes: 0

Views: 8488

Answers (2)

Jose Quijada
Jose Quijada

Reputation: 692

Regardin the EBUSY error, do one thing. Do console.log(process.cwd()) to see what directory Node process is in before it tries to delete the folder. If Node is in the same folder it is trying to delete, then it will issue the EBUSY error. I had that happen to me in a Node.js application I'm developing. The solution was to change directory (process.chdir(new directory)) to something other than the one I'm trying to delete before I attempt to delete it, and problem solved. This happened on Windows by the way.

Upvotes: 2

alejandromav
alejandromav

Reputation: 943

To remove it syncronously:

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

Upvotes: 0

Related Questions