Open In App

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:

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




// 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




// 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


Article Tags :