How to use the unlink function from fs

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

61
62
63
64
65
66
67
68
69
70
```js
const fs = require('fs/promises');

(async function(path) {
  try {
    await fs.unlink(path);
    console.log(`successfully deleted ${path}`);
  } catch (error) {
    console.error('there was an error:', error.message);
  }
fork icon371
star icon0
watch icon78

28
29
30
31
32
33
34
35
36
37
* fs.rename(): 重命名文件或文件夹。
* fs.rmdir(): 删除文件夹。
* fs.stat(): 返回文件(通过传入的文件名指定)的状态。相关方法:fs.fstat()、fs.lstat()。
* fs.symlink(): 新建文件的符号链接。
* fs.truncate(): 将传递的文件名标识的文件截断为指定的长度。相关方法:fs.ftruncate()。
* fs.unlink(): 删除文件或符号链接。
* fs.unwatchFile(): 停止监视文件上的更改。
* fs.utimes(): 更改文件(通过传入的文件名指定)的时间戳。相关方法:fs.futimes()。
* fs.watchFile(): 开始监视文件上的更改。相关方法:fs.watch()。
* fs.writeFile(): 将数据写入文件。相关方法:fs.write()。
fork icon192
star icon570
watch icon16

+ 5 other calls in file

270
271
272
273
274
275
276
277
278
279
  assert.strictEqual(err.code, 'ENOENT');
  assert.strictEqual(err.syscall, 'unlink');
  return true;
};

fs.unlink(nonexistentFile, common.mustCall(validateError));

assert.throws(
  () => fs.unlinkSync(nonexistentFile),
  validateError
fork icon42
star icon19
watch icon0

198
199
200
201
202
203
204
205
206
207
208
209
};


exports.deleteAsset = async function (req, res) {
  const assetsPath = path.join(process.cwd(), "./assets/");


  await fs.unlink(assetsPath + req.body.asset, async (err) => {
    if (err) {
      res.send({ state: "error", message: err.message });
    } else {
      await challenges.updateMany({ file: req.body.asset }, { file: "" });
fork icon5
star icon26
watch icon2

33
34
35
36
37
38
39
40
41
42
    this.parent = parent;
}

deleteAsync(options: any): IAsyncAction {
    return new IAsyncOperation((res, rej) => {
        fs.unlink(this.path, (err) => {
            if(err) rej(err);
            res();
        })
    });
fork icon1
star icon12
watch icon3

+ 3 other calls in file

5
6
7
8
9
10
11
12
13
14
  });
}

function unlinkAsync(path) {
  return new Promise((resolve, reject) => {
    unlink(path, err => (err ? reject(err) : resolve()));
  });
}

module.exports = {
fork icon0
star icon12
watch icon4

1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
                  console.log(`An unexpected error occurred: ${error}`);
                }
            }
        });
    }).on('error', (err) => {
        fs.unlink(filename, () => {
            console.error(`Error downloading file: ${err.message}`);
        });
    });
});
fork icon6
star icon6
watch icon1

+ 2 other calls in file

71
72
73
74
75
76
77
78
79
80
81
82


  const name = path + '.DELETE.' + crypto.randomBytes(16).toString('hex')
  fs.rename(path, name, er => {
    if (er)
      return cb(er)
    fs.unlink(name, cb)
  })
}


/* istanbul ignore next */
fork icon3
star icon2
watch icon0

105
106
107
108
109
110
111
112
113
114
    }
    fs.readFile(`temp/${senderid}-subdo-${args[1]}.txt`, 'utf8', function(err, data) {
        if (err) throw err;
        client.reply(from, data, id)
    });
    fs.unlink(`temp/${senderid}-subdo-${args[1]}.txt`, function (err) {
        if (err) throw err;
    });
    break
case 'payload': case 'pload':
fork icon1
star icon3
watch icon0

+ 4 other calls in file

161
162
163
164
165
166
167
168
169
170
	return res.redirect('/profile');
}
const tempPath = req.file.path;
const targetPath = path.join(__dirname, `./public/uploads/${req.file.filename}.png`);
if (path.extname(req.file.originalname).toLowerCase() != '.png') {
	return fs.unlink(tempPath, err => {
		if (err) return console.log(err);
		res.redirect('/profile');
	});
}
fork icon1
star icon0
watch icon1

26
27
28
29
30
31
32
33
34
35
    if(err){
        console.error(err);
        return;
    }

    fs.unlink(path, (err) => {
        if(err) throw err;
        console.log(`${filename} was deleted`);
    });
});
fork icon0
star icon1
watch icon1

725
726
727
728
729
730
731
732
733
        if (entryStat.isDirectory()) {
            await removeDir(entryPath);
            await fs.rmdir(entryPath);
        }
        else if (entryStat.isFile()) {
            await fs.unlink(entryPath);
        }
    }
};
fork icon0
star icon1
watch icon1

1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
    const err = await restore(newPath, (p) =>
      load_plugins.loadAndSaveNewPlugin(p)
    );
    if (err) req.flash("error", err);
    else req.flash("success", req.__("Successfully restored backup"));
    fs.unlink(newPath, function () {});
    res.redirect(`/admin`);
  })
);

fork icon194
star icon0
watch icon31

744
745
746
747
748
749
750
751
752
753
reportFunc.call(_this, req, res, fullmodelid, params, data, tmppath, function(err, rsltpath){
  if(err) return done(err, null);
  var dispose = function (disposedone) {
    fs.close(tmpfd, function () {
      fs.unlink(rsltpath, function (err) {
        fs.unlink(tmppath, function (err) {
          if (disposedone) disposedone();
        });
      });
    });
fork icon5
star icon5
watch icon2

+ 2 other calls in file

67
68
69
70
71
72
73
74
75
76
    });
}))
    .then(() => {
        return true;
    });
fs.unlink(path.join(__dirname, `../public/postVideo/${postData.postVideo}`), (err) => {
    if (err) {
        return false;
    } else {
        return true;
fork icon0
star icon2
watch icon1

+ 6 other calls in file

26
27
28
29
30
31
32
33
34
35
const commentData = await commentModel.findById(
    {
        _id: commentId
    }
);
fs.unlink(path.join(__dirname, `../public/commentImg/${commentData.commentImg}`), (err) => {
    if (err) {
        return false;
    } else {
        return true;
fork icon0
star icon2
watch icon1

604
605
606
607
608
609
610
611
612
613
  );

  await fs.writeFile(filename, await inter.handle());
  await sftp.fastPut(filename, conf.path);

  await fs.unlink(filename);
} catch (err) {
  this.log(
    "error",
    "findSCPConnectionsForSCP",
fork icon1
star icon8
watch icon2

429
430
431
432
433
434
435
436
    });
};


const clearImage = (filePath) => {
  filePath = path.join(__dirname, "..", filePath);
  fs.unlink(filePath, (err) => console.log(err));
};
fork icon0
star icon2
watch icon1

66
67
68
69
70
71
72
73
74
75
    newBook.save();
  } catch (err) {
    res.status(500).json({ msg: "server error" });
    return;
  }
  fs.unlink(req.file.path, (err) => {
    if (err) throw err;
  });
  res.status(201).json({ msg: "success" });
};
fork icon0
star icon0
watch icon1

517
518
519
520
521
522
523
524
525
526
const newFileName = filename.replace(/\s/g, '_');
const filePath = process.cwd() + '/tmp/' + newFileName; // Set the file path
fs.access(filePath, fs.constants.F_OK, (err) => {
    if (!err) {
        // File already exists, delete it before overwriting
        fs.unlink(filePath, (err) => {
            if (err) {
                return next(new ErrorHandler('File upload failed', 500));
            }
            // console.log('Deleted existing file');
fork icon0
star icon0
watch icon1

+ 2 other calls in file