Open In App

Node.js fsPromises.rmdir() Method

The fsPromises.rmdir() method is used to delete a directory at the given path. It can also be used recursively to remove nested directories. It resolves the Promise with no arguments upon success.

Syntax:



fsPromises.rmdir( path, options )

Parameters: This method accept two parameters as mentioned above and described below:

Below examples illustrate the fsPromises.rmdir() method in Node.js:



*Create “gfg_directory” in the machine path to run the code properly.

Example 1: This example uses fsPromises.rmdir() method to delete a directory.

Filename: index.js




// Node.js program to demonstrate the 
// fsPromises.rmdir() method 
  
// Import the filesystem module 
const fs = require('fs');
const fsPromises = require('fs').promises;
  
// Get the current filenames 
// in the directory 
getCurrentFilenames();
  
(async function main() {
    try {
  
        fsPromises.rmdir("gfg_directory")
        console.log("Folder Deleted!");
  
        // Get the current filenames 
        // in the directory to verify 
        getCurrentFilenames();
  
    } catch (err) {
        console.error(err);
    }
})();
  
// Function to get current filenames 
// in directory 
function getCurrentFilenames() {
    console.log("\nCurrent filenames:");
    fs.readdirSync(__dirname).forEach(file => {
        console.log(file);
    });
    console.log("\n");
}

Output:

Current filenames:
gfg_directory
index.js
package.json
Folder Deleted!

Current filenames:
index.js
package.json

Example 2: This example uses fsPromsies.rmdir() method with the recursive parameter to delete nested directories.




// Node.js program to demonstrate the 
// fsPromises.rmdir() method 
  
// Import the filesystem module 
const fs = require('fs');
const fsPromises = require('fs').promises;
  
// Get the current filenames 
// in the directory 
(async function main() {
    try {
        getCurrentFilenames();
        // Trying to delete nested directories 
        // without the recursive parameter 
        fsPromises.rmdir("gfg_directory", { recursive: false })
        console.log("Non Recursive: Directories Deleted!");
  
        // Using the recursive option to delete 
        // multiple directories that are nested 
        fsPromises.rmdir("gfg_directory", { recursive: true })
        console.log("Recursive: Directories Deleted!");
  
        // Get the current filenames 
        // in the directory to verify 
        getCurrentFilenames();
  
        // Function to get current filenames 
        // in directory 
        function getCurrentFilenames() {
            console.log("\nCurrent filenames:");
            fs.readdirSync(__dirname).forEach(file => {
                console.log(file);
            });
            console.log("\n");
        }
  
    } catch (err) {
        console.error(err);
    }
})();

Output:

Current filenames:
gfg_directory
index.js
package.json

[Error: ENOTEMPTY: directory not empty,  
rmdir 'F:\tutorials\nodejs-fs-rmdir\directory_one'] {
 errno: -4051,
 code: 'ENOTEMPTY',
 syscall: 'rmdir',
 path: 'F:\\tutorials\\nodejs-fs-rmdir\\directory_one'
}
Recursive: Directories Deleted!

Current filenames:
index.js
package.json

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


Article Tags :