EN
Node.js - delete files (fs.unlink() method)
0
points
In this article, we would like to show you how to delete files in Node.js.
To delete a file with the File System module we use the fs.unlink()
method.
Below we create a script that deletes newFile.txt
which is in the same directory. Then we run our script using the following command: node file.js
.
Practical example:
var fs = require('fs');
fs.unlink('newFile.txt', function (err) {
if (err) throw err;
console.log('file deleted.');
});
Before:
After:
Note:
If the file you want to delete is in a different directory just specify relative path to the file as a
fs.writeFile()
first argument instead of just file name.
Practical example:
var fs = require('fs');
fs.unlink('./Folder/newFile.txt', function (err) {
if (err) throw err;
console.log('file deleted.');
});
Before:
After: