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:
- 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 that 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 that 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 that 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 milliseconds.
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
JavaScript
const fs = require( 'fs' );
const fsPromises = require( 'fs' ).promises;
getCurrentFilenames();
(async function main() {
try {
fsPromises.rmdir( "gfg_directory" )
console.log( "Folder Deleted!" );
getCurrentFilenames();
} catch (err) {
console.error(err);
}
})();
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.
JavaScript
const fs = require( 'fs' );
const fsPromises = require( 'fs' ).promises;
(async function main() {
try {
getCurrentFilenames();
fsPromises.rmdir( "gfg_directory" , { recursive: false })
console.log( "Non Recursive: Directories Deleted!" );
fsPromises.rmdir( "gfg_directory" , { recursive: true })
console.log( "Recursive: Directories Deleted!" );
getCurrentFilenames();
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
Last Updated :
05 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...