How to use the rm function from fs

Find comprehensive JavaScript fs.rm code examples handpicked from public code repositorys.

fs.rm is a Node.js file system method used to delete files and directories recursively.

239
240
241
242
243
244
245
246
247
248
  return res.status(500).json({ success: false, message: "internal server error. please try again later" });    
} else {
  console.log("Sent:", fileName, "at", new Date().toString());
  /*
  // Se comenta esta porción porque el archivo no debe eliminarse. Otro dispositivo podría llamar despuès a la ruta "/download" y no encontrar el archivo. 
  fs.rm(`./files/${fileName}`, { recursive:true }, (err) => {
      if(err){
          // File deletion failed
          console.error(err.message);
          return;
fork icon0
star icon0
watch icon1

60
61
62
63
64
65
66
67
68
    fs.writeFile(`${userPath}/${req.body.username}/info/userInfo.json`, `${config}\n`, { flag: "a" }, (err) => { if (err) { errNum++ } })
    if (errNum === null) {
        setCookie(res, "webnote", req.body.username, 60 * 24)
        res.send({ code: 1, msg: "注册成功。" })
    } else {
        fs.rm(`${userPath}/${req.body.username}`, { recursive: true, maxRetries: 10 }, function (err) { })
        res.send({ code: 0, msg: "服务器繁忙,请稍后重试。" })
    }
}
fork icon0
star icon0
watch icon1

+ 2 other calls in file

How does fs.rm work?

The fs.rm method is used to remove a file or directory using the file system module in Node.js. It provides options to specify the recursive nature of the operation, permissions and error handling.

583
584
585
586
587
588
589
590
591
592

if(!err){
  if(results.affectedRows == 0){
    res.status(404).json({message:'id não encontrado'})
  }
   fs.rm(`pdfs/escala-coroinha-pdf/${nomepdf[0].escala_coroinha}`, { recursive:true }, (err) => {
    if(err){
        // File deletion failed
        console.error(err.message);
        return;
fork icon0
star icon0
watch icon1

227
228
229
230
231
232
233
234
235
236
 * @param  {[type]} execArgv list of string arguments passed to the worker process
 */
onWorkerStart: function (cid, caps, specs, args, execArgv) {
  const dir = "test/.artifacts";
  // delete directory recursively
  fs.rm(dir, { recursive: true }, (err) => {
    if (err) {
      throw err;
    }
    console.log(`${dir} is deleted!`);
fork icon0
star icon0
watch icon1

+ 2 other calls in file

Ai Example

1
2
3
4
5
6
const fs = require("fs");

fs.rm("example.txt", (err) => {
  if (err) throw err;
  console.log("File removed");
});

In this example, we use fs.rm to remove a file named example.txt. If the file is successfully removed, we log a message saying so to the console. If an error occurs during the removal process, we throw the error.

204
205
206
207
208
209
210
211
212
213
214
    permission: 'FileSystemWrite',
    resource: path.toNamespacedPath(path.join(absoluteProtectedFolder, 'any-file')),
  }));
}


// fs.rm
{
  assert.throws(() => {
    fs.rmSync(blockedFolder, { recursive: true });
  }, common.expectsError({
fork icon0
star icon0
watch icon0