English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
To delete files in Node.js, you can use Node.js FS unlink(path, callback) for asynchronous file operations, and unlinkSync(path) for synchronous file operations. This Node.js tutorial provides a step-by-step guide to deleting files with the node fs module and detailed examples.
The following is a step-by-step guide to deleting files programmatically in Node.js:
Steps1Include the file system module in your Node.js program
var fs = require('fs); |
We will use this module unlink() and its unlinkSync() feature.
Steps2Use the unlink() function to asynchronously delete a file. The syntax is provided below.
fs.unlink(filePath, callbackFunction) |
After attempting to delete the file, the callback function (if any) will be called with an error (as a parameter).
To delete files synchronously, use the unlinkSync() function. The syntax is provided below.
fs.unlinkSync(filePath) |
Among which,filePath is a String representing the path of the file to be deleted.
For this example, please ensure that there is a file named 'sample.txt' next to the node.js example program.
Create the following Node.js program, deleteFile.js, to delete files in Node.js.
// Import the fs module var fs = require('fs'); // Delete the file named 'sample.txt' fs.unlink('sample.txt', function(err) { if (err) throw err; // If there is no error, the file has been successfully deleted console.log('File deleted!'); });
Run the program using the node command in the terminal or command prompt.
Terminal Output
$ node deleteFile.js File deleted!
The file has been successfully deleted.
Create the following Node.js program to synchronously delete a file in Node.js. This will be very useful if the statements next to the delete operation depend on the deleted file. The unlinkSync() function ensures that the file is deleted (if it exists) before executing the subsequent statements.
// Import the fs module var fs = require('fs'); // Delete the file named 'sample.txt' Synchronously fs.unlinkSync('sample.txt'); console.log('File deleted!');
Run the program using the node command in the terminal or command prompt.
Terminal Output
$ node deleteFileSynchronously.js File deleted!
The file has been successfully deleted.
For this example, please make sure there is no file named 'sample' next to the node.js example program.11.txt” file. We will simulate the condition, trying to delete the file that does not exist at this location.
// Import the fs module var fs = require('fs'); // Delete the file named 'sample.txt' fs.unlink('sample11.txt', function (err) { if (err) throw err; // If there is no error, the file has been successfully deleted console.log('File deleted!'); });
Run the program using the node command in the terminal or command prompt.
Terminal Output
$ node deleteFile2.js /home/arjun/workspace/nodejs/deleteFile2.js:6 if (err) throw err; ^ Error: ENOENT: no such file or directory, unlink 'sample11.txt'
An error will be triggered if the file does not exist, prompting 'No such file or directory'.
In the end of this Node.js tutorial – Node FS, we learned how to use the built-in Node FS (File System) module to delete a file in Node.js.