Languages
[Edit]
EN

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

0 points
Created by:
Blessing-D
574

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()

Alternative titles

  1. Node.js - remove files (fs.unlink() method)
  2. Node.js - delete file using fs.unlink() method
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

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