Node.js | fs.rmdir() Method

The fs.rmdir() method is used to delete a directory at the given path. It can also be used recursively to remove nested directories.

Syntax:

fs.rmdir( path, options, callback )

Parameters: This method accept three 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 specifed 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 when 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 to wait in milliseconds before the operation is retried. This option is ignored if the recursive option is not set to true. The default value is 100 miliseconds.
  • callback: It is the function that would be called when the method is executed.
    • err: It is an error that would be thrown if the operation fails.

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

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node.js program to demonstrate the
// fs.rmdir() method
  
// Import the filesystem module
const fs = require('fs');
  
// Get the current filenames
// in the directory
getCurrentFilenames();
  
fs.rmdir("directory_one", () => {
  console.log("Folder 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");
}

chevron_right


Output:

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

Current filenames:
index.js
package.json

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node.js program to demonstrate the
// fs.rmdir() method
  
// Import the filesystem module
const fs = require('fs');
  
// Get the current filenames
// in the directory
getCurrentFilenames();
  
// Trying to delete nested directories
// without the recursive parameter
fs.rmdir("directory_one", {
  recursive: false,
}, (error) => {
  if (error) {
    console.log(error);
  }
  else {
    console.log("Non Recursive: Directories Deleted!");
  }
});
  
// Using the recursive option to delete
// multiple directories that are nested
fs.rmdir("directory_one", {
  recursive: true,
}, (error) => {
  if (error) {
    console.log(error);
  }
  else {
    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");
}

chevron_right


Output:

Current filenames:
directory_one
index.js
package.json


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

Current filenames:
index.js
package.json

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




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.