Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js remove file

Tags:

node.js

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?

like image 721
Mark Avatar asked Mar 15 '11 16:03

Mark


People also ask

How do you delete a file in node JS?

In Node. js, you can use the fs. unlink() method provided by the built-in fs module to delete a file from the local file system.

Which method is used remove file in NodeJS?

unlink() method is used to remove a file or symbolic link from the filesystem.

Can JavaScript delete files?

You can't delete a file when running JavaScript from the browser, but you can do it when running JavaScript from a server environment like NodeJS. When you need to delete a file using NodeJS, You can use the fs. unlink() or fs.


2 Answers

I think you want to use fs.unlink.

More info on fs can be found here.

like image 118
Nick Avatar answered Oct 20 '22 19:10

Nick


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); 
like image 34
sourcecode Avatar answered Oct 20 '22 18:10

sourcecode