Node.js fs.rm() Method
Last Updated :
03 Feb, 2021
The fs.rm() method is used to delete a file at the given path. It can also be used recursively to remove directories.
Syntax:
fs.rm( path, options, callback );
Parameters: This method accepts three parameters as mentioned above and described below:
- path: It holds the path of the file 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 as follows:
- force: It is a boolean value. Exceptions will be ignored if the path does not exist.
- 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.
- 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.rm() method in Node.js:
Example 1: This example uses fs.rm() method to delete a file.
Filename: index.js
Javascript
let fs = require( 'fs' );
getCurrentFilenames();
fs.rm( './dummy.txt' , { recursive: true }, (err) => {
if (err){
console.error(err.message);
return ;
}
console.log( "File deleted successfully" );
getCurrentFilenames();
})
function getCurrentFilenames() {
console.log( "\nCurrent filenames:" );
fs.readdirSync(__dirname).forEach(file => {
console.log(file);
});
console.log( "" );
}
|
Run the index.js file using the following command:
node index.js
Output:
Current filenames:
dummy.txt
index.js
node_modules
package-lock.json
package.json
File deleted successfully
Current filenames:
index.js
node_modules
package-lock.json
package.json
Example 2: This example uses fs.rm() method with the recursive parameter to delete directories.
Filename: index.js
Javascript
const fs = require( 'fs' );
getCurrentFilenames();
fs.rm( "./build" , { recursive: false }, (err) => {
if (err) {
console.error(err);
}
else {
console.log( "Non Recursive: Directory Deleted!" );
}
});
fs.rm( "./build" , { recursive: true }, (err) => {
if (err) {
console.error(err);
}
else {
console.log( "Recursive: Directory Deleted!" );
getCurrentFilenames();
}
});
function getCurrentFilenames() {
console.log( "\nCurrent filenames:" );
fs.readdirSync(__dirname).forEach(file => {
console.log(file);
});
console.log( "\n" );
}
|
Run the index.js file using the following command:
node index.js
Output:
Current filenames:
build
index.js
node_modules
package-lock.json
package.json
SystemError [ERR_FS_EISDIR]: Path is a directory: rm returned EISDIR
(is a directory) ./build
at internal/fs/utils.js:688:23
at FSReqCallback.oncomplete (fs.js:184:5) {
code: 'ERR_FS_EISDIR',
info: {
code: 'EISDIR',
message: 'is a directory',
path: './build',
syscall: 'rm',
errno: 21
},
errno: [Getter/Setter: 21],
syscall: [Getter/Setter: 'rm'],
path: [Getter/Setter: './build']
}
Recursive: Directory Deleted!
Current filenames:
index.js
node_modules
package-lock.json
package.json
Reference:https://nodejs.org/api/fs.html#fs_fs_rm_path_options_callback
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...