Languages
[Edit]
EN

Node.js - delete files (fs.unlink() method)

0 points
Created by:
Blessing-D
274

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:

Node.js - deleting file with fs.unlink()

After:

Node.js - deleting file with fs.unlink()

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:

Node.js - deleting file with fs.unlink()

After:

Node.js - deleting file with fs.unlink()

Node.js - file system module

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join