node.js remove file


Question

How do I delete a file with node.js?

http://nodejs.org/api/fs.html#fs_fs_rename_oldpath_newpath_callback

I don't see a remove command?

1
512
2/22/2013 1:51:07 PM

Accepted Answer

I think you want to use fs.unlink.

More info on fs can be found here.

741
8/11/2015 3:58:06 PM

You can call fs.unlink(path, callback) for Asynchronous unlink(2) or fs.unlinkSync(path) for Synchronous unlink(2).
Where path is file-path which you want to remove.

For example we want to remove discovery.docx file from c:/book directory. So my file-path is c:/book/discovery.docx. So code for removing that file will be,

var fs = require('fs');
var filePath = 'c:/book/discovery.docx'; 
fs.unlinkSync(filePath);

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon