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?
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.
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.
The fs. rm() method is used to delete a file at the given path. It can also be used recursively to remove directories.
rmdir() Method - GeeksforGeeks.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With