![]() |
VOOZH | about |
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:
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
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
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