Hi whoever come here,
I have been coding for quite long, and this is the first time I created a blog for my studies. Here are some of the results of my researches and learning hours on the subject of Node js fs module.
1. fs.unlink(path, callback)
This function is used to deletes a file or symbolic link Asynchronously. When a callback is done, no argument except a possible exception will be given. It will not block your code, and will call a callback function once the file has been removed.
// Assuming that 'path/file.txt' is a regular file.
fs.unlink('path/file.txt', (err) => {
if (err) throw err;
console.log('path/file.txt was deleted');
});
2. fs.unlinkSync(path)
Not like unlink, fs.unlinkSync(path) delete file synchronous manner.It is synchronously, so the synchronous call will cause your code to block and wait until the file has been removed.
const fs = require('fs')
const path = './file.txt'
try {
fs.unlinkSync(path)
} catch(err) {
console.error(err)
}
+ References:
https://flaviocopes.com/how-to-remove-file-node/ http://www.technicalkeeda.com/nodejs-tutorials/how-to-delete-file-using-nodejs