VOOZH about

URL: https://www.geeksforgeeks.org/node-js/node-js-fs-unlinksync-method/

⇱ Node.js fs.unlinkSync() Method - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Node.js fs.unlinkSync() Method

Last Updated : 8 Jan, 2025

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.

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. 

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

Comment

Explore