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 !