June 2014
Intermediate to advanced
696 pages
38h 52m
English
Another common task when working with files is deleting them to clean up data or make more room on the file system. To delete a file from Node.js, use one of the following commands:
fs.unlink(path, callback)fs.unlinkSync(path)
The unlinkSync(path) function returns true or false, depending on whether the delete was successful. The asynchronous unlink() call passes back an error value to the callback function if an error is encountered when deleting the file.
The following code snippet illustrates the process of deleting a file named new.txt by using the unlink() asynchronous fs call:
fs.unlink("new.txt", function(err){ console.log(err ? "File Delete Failed" : "File Deleted");});
Read now
Unlock full access