Node.js fs.rmdirSync() Method

11 Oct, 2021
The fs.rmdirSync() method is used to synchronously delete a directory at the given path. It can also be used recursively to remove nested directories by configuring the options object. It returns undefined.Syntax:  
fs.rmdirSync( path, options )
Parameters: This method accept two parameters as mentioned above and described below:  
path: It holds the path of the directory that has to be removed. It can be a String, Buffer or URL.
options: It is an object that can be used to specify optional parameters that will affect the operation. It has three optional parameters:recursive: It is a boolean value which specifies if recursive directory removal is performed. In this mode, errors are not reported if the specified path is not found and the operation is retried on failure. The default value is false.maxRetries: It is an integer value which specifies the number of times Node.js will try to perform the operation, if it fails due to any error. The operations are performed after the given retry delay. This option is ignored if the recursive option is not set to true. The default value is 0.retryDelay: It is an integer value which specifies the time in milliseconds to wait before the operation is retried. This option is ignored if the recursive option is not set to true. The default value is 100 milliseconds.
recursive: It is a boolean value which specifies if recursive directory removal is performed. In this mode, errors are not reported if the specified path is not found and the operation is retried on failure. The default value is false.
maxRetries: It is an integer value which specifies the number of times Node.js will try to perform the operation, if it fails due to any error. The operations are performed after the given retry delay. This option is ignored if the recursive option is not set to true. The default value is 0.
retryDelay: It is an integer value which specifies the time in milliseconds to wait before the operation is retried. This option is ignored if the recursive option is not set to true. The default value is 100 milliseconds.
Below examples illustrate the fs.rmdirSync() method in Node.js:Example 1: This example uses fs.rmdirSync() method to delete a directory. 
javascript
// Node.js program to demonstrate the// fs.rmdirSync() method  // Import the filesystem moduleconst fs = require('fs');  // Get the current filenames// in the directorygetCurrentFilenames();  fs.rmdirSync("directory_one");console.log("Folder Deleted!");  // Get the current filenames// in the directory to verifygetCurrentFilenames();  // Function to get current filenames// in directoryfunction getCurrentFilenames() {  console.log("\nCurrent filenames:");  fs.readdirSync(__dirname).forEach(file => {    console.log(file);  });  // console.log("\n");}
Output:  
Current filenames:
directory_one
index.js
package.json
Folder Deleted!

Current filenames:
index.js
package.json
Example 2: This example uses fs.rmdirSync() method with the recursive parameter to delete nested directories. 
javascript
// Node.js program to demonstrate the// fs.rmdirSync() method  // Get the current filenames// in the directorygetCurrentFilenames();  // Using the recursive option to delete// multiple directories that are nestedfs.rmdirSync("directory_one", {  recursive: true,});console.log("Directories Deleted!");  // Get the current filenames// in the directory to verifygetCurrentFilenames();    // Function to get current filenames// in directoryfunction getCurrentFilenames() {  console.log("\nCurrent filenames:");  fs.readdirSync(__dirname).forEach(file => {    console.log(file);  });  console.log("\n");}
Output:  
Current filenames:
directory_one
index.js
package.json


Directories Deleted!

Current filenames:
index.js
package.json
Reference: https://nodejs.org/api/fs.html#fs_fs_rmdirsync_path_options 
arorakashish0911
Node.js-fs-module
Picked
Node.js
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.