5

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?

Community
  • 1
  • 1
Man
  • 742
  • 1
  • 6
  • 23

1 Answers1

13

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.

zvava
  • 101
  • 1
  • 3
  • 14
abdulbarik
  • 6,101
  • 5
  • 38
  • 59
  • 1
    If I unlink the file then, of course, it will be deleted from targeted folder – abdulbarik Oct 10 '16 at 19:39
  • You can also see here http://stackoverflow.com/questions/18052762/remove-directory-which-is-not-empty by SharpCoder answer – abdulbarik Oct 10 '16 at 19:39
  • I would look at [the documentation you linked](http://man7.org/linux/man-pages/man2/unlink.2.html). – skypjack Oct 10 '16 at 19:40
  • It also says same `unlink() deletes a name from the filesystem. If that name was the last link to a file and no processes have the file open, the file is deleted and the space it was using is made available for reuse.` my word is just defference – abdulbarik Oct 10 '16 at 19:42
  • And this leads us to my first comment. Loop closed. – skypjack Oct 10 '16 at 19:43
  • 4
    @skypjack is this a case of being nitpicky or trying to confuse people? can you be more specific what unlink does if not deleting files ? your comments seem like cheap trolling. – Edeph Sep 13 '18 at 14:39