Open In App

Node.js fs.unlinkSync() Method

Last Updated : 08 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

The fs.unlinkSync() method is used to synchronously 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.
Syntax:  

fs.unlinkSync( path )

Parameters: This method accepts one parameter as mentioned above and described below:  

  • path: It is a string, Buffer or URL which represents the file or symbolic link which has to be removed.

Below examples illustrate the fs.unlinkSync() method in Node.js:
Example 1: This example removes a file from the filesystem.

javascript




// Node.js program to demonstrate the
// fs.unlinkSync() method
  
// Import the filesystem module
const fs = require('fs');
  
// Get the files in current directory
// before deletion
getFilesInDirectory();
  
// Delete readme.md
fs.unlinkSync("readme.md");
console.log("\nFile readme.md is deleted");
  
// Get the files in current directory
// after deletion
getFilesInDirectory();
  
// Function to get current filenames
// in directory with specific extension
function getFilesInDirectory() {
  console.log("\nFiles present in directory:");
  let files = fs.readdirSync(__dirname);
  files.forEach(file => {
    console.log(file);
  });
}


Output: 

Files present in directory:
index.html
index.js
package.json
readme.md

File readme.md is deleted

Files present in directory:
index.html
index.js
package.json

Example 2: This example removes a symbolic link from the filesystem. 

javascript




// Node.js program to demonstrate the
// fs.unlinkSync() method
  
// Import the filesystem module
const fs = require('fs');
  
// Creating symlink to file
fs.symlinkSync(__dirname + "\\readme.md", "symlinkToReadme");
console.log("\nSymbolic link to readme.md created");
  
// Function to get current filenames
// in directory with specific extension
getFilesInDirectory();
  
// Deleting symbolic link to readme.md
fs.unlinkSync("symlinkToReadme");
console.log("\nSymbolic link to readme.md deleted")
  
getFilesInDirectory();
  
// Function to get current filenames
// in directory with specific extension
function getFilesInDirectory() {
  console.log("\nFiles present in directory:");
  let files = fs.readdirSync(__dirname);
  files.forEach(file => {
    console.log(file);
  });
}


Output: 

Symbolic link to readme.md created

Files present in directory:
index.html
index.js
package.json
readme.md
symlinkToReadme

Symbolic link to readme.md deleted

Files present in directory:
index.html
index.js
package.json
readme.md

Reference: https://nodejs.org/api/fs.html#fs_fs_unlinksync_path



Previous Article
Next Article

Similar Reads

Node.js Connect Mysql with Node app
In this article, we will learn how to connect the Mysql database to the NodeJs application. Before we go deep into the coding part, let’s get a brief introduction about these technologies: NodeJs: An open-source platform for executing javascript code on the server side. Also a javascript runtime built on Chrome's V8 JavaScript engine. It can be dow
3 min read
Node.js Automatic restart Node.js server with nodemon
We generally type following command for starting NodeJs server: node server.js In this case, if we make any changes to the project then we will have to restart the server by killing it using CTRL+C and then typing the same command again. node server.js It is a very hectic task for the development process. Nodemon is a package for handling this rest
1 min read
How to resolve 'node' is not recognized as an internal or external command error after installing Node.js ?
There are many different ways to install node.js on a computer. The simplest method to verify whether node.js has been properly installed in your computer is simply type node-v in the command prompt or Windows PowerShell. But many times, it happens, most commonly if you're a beginner, the command prompt prints the output something like this: 'node'
2 min read
Top 3 Best Packages Of Node.js that you should try being a Node.js Developer
Node.js is an open-source and server-side platform built on Google Chrome's JavaScript Engine (V8 Engine). Node.js has its own package manager called NPM( Node Package Manager) which has very useful and incredible libraries and frameworks that makes our life easier as a developer to work with Node.js. The 3 Best Packages of Node.js that you should
4 min read
Javascript Program For Inserting A Node After The N-th Node From The End
Insert a node x after the nth node from the end in the given singly linked list. It is guaranteed that the list contains the nth node from the end. Also 1 <= n. Examples: Input : list: 1->3->4->5 n = 4, x = 2 Output : 1->2->3->4->5 4th node from the end is 1 and insertion has been done after this node. Input : list: 10->8
5 min read
Javascript Program For Swapping Kth Node From Beginning With Kth Node From End In A Linked List
Given a singly linked list, swap kth node from beginning with kth node from end. Swapping of data is not allowed, only pointers should be changed. This requirement may be logical in many situations where the linked list data part is huge (For example student details line Name, RollNo, Address, ..etc). The pointers are always fixed (4 bytes for most
5 min read
Node.js Http2ServerRequest.method Method
The Http2ServerRequest.method is an inbuilt application programming interface of class Http2ServerRequest within the http2 module which is used to get the string representation of the request method. Syntax: const request.method Parameters: This method does not accept any argument as a parameter. Return Value: This method returns the string represe
3 min read
Node.js http.IncomingMessage.method Method
The http.IncomingMessage.method is an inbuilt application programming interface of class Incoming Message within the inbuilt http module which is used to get the type of request method as a string. Syntax: request.method Parameters: This method does not accept any argument as a parameter. Return Value: This method returns the request type name as a
2 min read
Node.js URLSearchParams.has() Method
In URLSearchParams interface, the has() method returns a Boolean which tells us that if the parameter with input name exists it will return true, else false. Syntax: var Bool = URLSearchParams.has(name) Returns: True - if name present, else it will return False. Parameters: name - Input the name of the parameter. Example1: let url = new URL('https:
1 min read
Node.js hmac.update() Method
The hmac.update() method is an inbuilt method of class HMAC within the crypto module which is used to update the data of hmac object. Syntax: hmac.update(data[, inputEncoding])Parameters: This method takes the following two parameters: data: It can be of string, Buffer, TypedArray, or DataView type. It is the data that is passed to this function.in
2 min read