Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can fs.unlink() delete a empty or non empty folder?

I am new to Node.js.

const fs = require('fs');

fs.unlink('/tmp/hello', (err) => {
  if (err) throw err;
  console.log('successfully deleted /tmp/hello');
});

This is some code that I copied from a node.js document file system intro example.

But, I am confused. Can unlink() delete a folder or not?

I have tried but it doesn't work.

So, can unlink() delete a folder or not?

like image 250
Man Avatar asked Oct 10 '16 17:10

Man


People also ask

What is the use of FS unlink () method?

The fs. unlink() method is used to remove a file or symbolic link from the filesystem. This function does not work on directories, therefore it is recommended to use fs. rmdir() to remove a directory.

What is the function to remove a non-empty directory?

The non-empty directory means the directory with files or subdirectories. We can delete the directory by using the Delete() method of the Directory class.

Which FS operation is used to delete a file?

The fs. rm() method is used to delete a file at the given path. It can also be used recursively to remove directories.

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

rmdir() Method - GeeksforGeeks.


1 Answers

The fs.unlink(path, callback) function is used to delete a file not a folder.

To remove a folder you can use the fs.rmdir(path, callback) function instead.

like image 164
abdulbarik Avatar answered Oct 27 '22 15:10

abdulbarik