Node.js fs.rm() Method
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
// Import necessary modules let fs = require( 'fs' ); // List files before deleting getCurrentFilenames(); fs.rm( './dummy.txt' , { recursive: true }, (err) => { if (err){ // File deletion failed console.error(err.message); return ; } console.log( "File deleted successfully" ); // List files after deleting getCurrentFilenames(); }) // This will list all files in current directory 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
// Import the filesystem module const fs = require( 'fs' ); // List the files in current directory getCurrentFilenames(); // Trying to delete directory without the recursive parameter fs.rm( "./build" , { recursive: false }, (err) => { if (err) { console.error(err); } else { console.log( "Non Recursive: Directory Deleted!" ); } }); // Using the recursive option to delete directory fs.rm( "./build" , { recursive: true }, (err) => { if (err) { console.error(err); } else { console.log( "Recursive: Directory Deleted!" ); // List files after delete getCurrentFilenames(); } }); // List all files in current directory 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
Please Login to comment...