How to delete a file in Node.js? Remove file with Node

Introduction

In this blog post, we will learn how to delete a file or symbolic link in Node.js. Node.js core fs module provides APIs to remove files or symlinks from the file system. fs.unlink() is used to asynchronously remove a file or symbolic link. fs.unlinkSync() is used to synchronously remove a file or symbolic link.

fs.unlink()

fs.unlink() is an asynchronous method used to delete a file. This method does not work on directories. It is recommended to use fs.rmdir() to delete a directory asynchronously

Syntax of fs.unlink()

fs.unlink(path, callback)

This method accepts two parameters as described below:

  • path: It holds the path of the file or symbolic link that has to be removed. It can be a string, buffer or URL.
  • callback: It is the function that will be called when the deleting operation is done.
    • err: error object. (If there is no error, error object holds null value)

Following code snippet illustrate the fs.unlink() in Node.js:

// import node fs module
const fs = require('fs');
 
// function to delete file named 'foo.txt' asynchronously
fs.unlink('foo.txt', function (err) {
    if (err) throw err;

    console.log('File deleted successfully.');
});

fs.unlinkSync()

fs.unlinkSync() is a synchronous method used to delete a file or symlink. This method does not work on directories, so it is recommended to use fs.rmdirSync() to delete a directory synchronously.

Syntax of fs.unlinkSync()

fs.unlinkSync(path)

This method accepts one parameter as described below:

  • path: The path of the file or symbolic link that has to be removed. It can be a string, buffer or URL.

Following code snippet illustrate the fs.unlinkSync() in Node.js:

// import node fs module
const fs = require('fs');
 
// function to delete file named 'foo.txt' synchronously
fs.unlinkSync('foo.txt');
console.log('File deleted successfully !');

Conclusion

It is advisable to use an asynchronous function instead of synchronous. As synchronous function blocks the event loop and should not be used in the production environment.

#Happy Coding #Topcode

#delete temp file node.js #delete multiple files node.js #delete file from s3 node js #mongodb delete document node js #elasticsearch delete document node js #cloudant delete document node js #remove file extension node.js #delete file if exists in node js #delete file node js #delete file using fs node js #delete a file node js #node js delete file after download #node js aws s3 delete file #delete file from s3 bucket node js #node js delete file contents #delete file in directory node js #node js delete file if exists #node js delete line from file #node.js delete text file #delete file node js fs #node js fs delete file if exists #fs delete file node js #delete file in nodejs #delete file in node #delete file using node js #how to delete file node js #how to delete file in node js using fs #delete file in node js #delete files using node js #node js file delete #delete local file node js #how to delete file in nodejs #how to delete file using node js #delete file from path node js #delete file nodejs #delete file with node js #how to delete file in node js #delete file from nodejs #delete files from folder node js remove file node js #remove file extension node.js #remove all files node js #remove file from folder node js #delete temp file node.js #remove files from directory node js #node js remove file if exists #node js remove file name from path #remove a file node js #remove file nodejs #node js remove content from file #node js remove file from directory #remove file node js fs #fs remove file node js #how to remove file in node js #remove file in node js #remove file in nodejs #node js remove line from file #node.js remove file path #node.js remove txt file #remove file using node js #remove file with node js #s3 delete file nodejs #aws s3 delete file nodejs #s3 delete folder nodejs

, , , , , ,

Related posts

Latest posts

Leave a Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Please disable your adblocker or whitelist this site!

How to whitelist website on AdBlocker?

How to whitelist website on AdBlocker?

  1. 1 Click on the AdBlock Plus icon on the top right corner of your browser
  2. 2 Click on "Enabled on this site" from the AdBlock Plus option
  3. 3 Refresh the page and start browsing the site