Node FS – Delete a File using unlink() or unlinkSync() functions
To delete a file in Node.js, Node FS unlink(path, callback) can be used for asynchronous file operation and
unlinkSync(path) can be used for synchronous file operation. In this Node.js Tutorial, a step by step guide is
provided to delete a file with node fs and well detailed examples.
Delete a File in Node.js – Node FS unlink()
Following is a step by step guide to delete a File programmatically in Node.js :
Step 1 : Include File System module to your Node.js program
var fs = require(‘fs‘);
We will use unlink() and unlinkSync() functions of this module.
Step 2 : Delete file asynchronously using unlink() function. Syntax is provided below
fs.unlink(filePath, callbackFunction)
Once an attempt is made to delete the file, callback function is called with error (as argument) if any.
To delete file synchronously, use unlinkSync() function. Syntax is provided below
fs.unlinkSync(filePath)
wherefilePath is a String that represents path of the file to be deleted.
Example: Delete file asynchronously using Node FS unlink() function
For this example, make sure there is a file named ‘sample.txt’ next to the node.js example program.
Create following Node.js program, deleteFile.js , to delete a file in Node.js.
deleteFile.js
// include node fs
module
var fs = require('fs');
//
// delete
includefile named
node fs module
'sample.txt'
var fs = require('fs');
fs.unlink('sample.txt',
function (err) {
if (err) throw err;
//
// delete file named
if no error, file has'sample.txt'
been deleted
fs.unlink('sample.txt', function (err) {
successfully
console.log('File
if (err) throw err;
deleted!');
}); // if no error, file has been deleted successfully
console.log('File deleted!');
});
Run the program using node command in terminal or command prompt.
Terminal Output
$ node deleteFile.js
File deleted!
$ node deleteFile.js
File deleted!
The file is successfully deleted.
Example: Delete file synchronously using Node FS unlinkSync() function
Create following Node.js program to delete a file in Node.js Synchronously. This is helpful if statements next to
the delete operation depend on the file you delete. unlinkSync() function makes sure that file is deleted(if it
exists) before the execution of subsequent statements.
deleteFileSynchronously.js
// include node fs
module
var fs = require('fs');
//
// delete
includefile named
node fs module
'sample.txt'
var fs = require('fs');
Synchronously
fs.unlinkSync('sample.t
xt');
// delete file named 'sample.txt' Synchronously
console.log('File
deleted!');
fs.unlinkSync('sample.txt');
console.log('File deleted!');
Run the program using node command in terminal or command prompt.
Terminal Output
$ node
deleteFileSynchronousl
y.js
File deleted!
$ node deleteFileSynchronously.js
File deleted!
The file is successfully deleted.
Example: File specified to delete is not present. (Error: ENOENT: no such file or
directory)
For this example, make sure there is no file named ‘sample11.txt’ next to the node.js example program. We will
simulate the condition that we tried to delete a file which is not present at the location.
deleteFile.js
// include node fs
module
// include node fs module
var fs = require('fs');
// delete file named 'sample.txt'
fs.unlink('sample11.txt', function (err) {
if (err) throw err;
// if no error, file has been deleted successfully
console.log('File deleted!');
});
Run the program using node command in terminal or command prompt.
Terminal Output
$ node deleteFile2.js
/home/arjun/workspace
/nodejs/deleteFile2.js:6
if (err) throw err;
$ node ^deleteFile2.js
/home/arjun/workspace/nodejs/deleteFile2.js:6
Error: ENOENT: no
suchif (err)orthrow
file directory,
err;
unlink 'sample11.txt'
^
Error: ENOENT: no such file or directory, unlink 'sample11.txt'
As the file is not present, an error is thrown saying ‘no such file or directory’.
Conclusion
Concluding this Node.js Tutorial – Node FS, we have learned to delete a File in Node.js using Node FS (File
System) built-in module.
Node.js
⊩ Node.js Tutorial
Get Started W ith Node.js
⊩ Install Node.js Ubuntu Linux
⊩ Install Node.js Windows
⊩ Node.js - Basic Example
⊩ Node.js - Command Line Arguments
⊩ Node.js - Modules
⊩ Node.js - Create a module
⊩ Node.js - Add new functions to Module
⊩ Node.js - Override functions of Module
⊩ Node.js - Override functions of Module
⊩ Node.js - Callback Function
⊩ Node.js - forEach
Express.js
⊩ Express.js Tutorial
⊩ What is Express.js?
⊩ Express.js Application Example
⊩ Install Express.js
⊩ Express.js Routes
⊩ Express.js Middleware
⊩ Express.js Router
Node.js Buffers
⊩ Node.js Buffer - Create, Write, Read
⊩ Node.js Buffer - Length
⊩ Node.js - Convert JSON to Buffer
⊩ Node.js - Array to Buffer
Node.js HTTP
⊩ Node.js - Create HTTP Web Server
⊩ Node.js - Redirect URL
Node.js MySQL
⊩ Node.js MySQL
⊩ Node.js MySQL - Connect to MySQL Database
⊩ Node.js MySQL - SELECT FROM
⊩ Node.js MySQL - SELECT WHERE
⊩ Node.js MySQL - ORDER BY
⊩ Node.js MySQL - INSERT INTO
⊩ Node.js MySQL - UPDATE
⊩ Node.js MySQL - DELETE
⊩ Node.js MySQL - Result Object
Node.js MongoDB
⊩ Node.js MongoDB
⊩ Node.js - Connect to MongoDB
⊩ Node.js - Create Database in MongoDB
⊩ Node.js - Drop Database in MongoDB
⊩ Node.js - Create Collection in MongoDB
⊩ Node.js - Delete Collection in MongoDB
⊩ Node.js - Insert Documents to MongoDB Collection
⊩ MongoError: failed to connect to server
Node.js Mongoose
⊩ Node.js Mongoose Tutorial
⊩ Node.js Mongoose - Installation
⊩ Node.js Mongoose - Connect to MongoDB
⊩ Node.js Mongoose - Define a Model
⊩ Node.js Mongoose - Insert Single Document to MongoDB
⊩ Node.js Mongoose - Insert Multiple Documents to MongoDB
Node.js URL
⊩ Node.js - Parse URL parameters
Node.js FS (File System)
⊩ Node FS
⊩ Node FS - Read a File
⊩ Node FS - Create a File
⊩ Node FS - Write to a File
⊩ Node FS - Append to a File
⊩ Node FS - Rename a File
⊩ Node FS - Delete a File
⊩ Node FS Extra - Copy a Folder
Node.js JSON
⊩ Node.js Parse JSON
⊩ Node.js Write JSON Object to File
Node.js Error Handling
⊩ Node.js Try Catch
Node.js Examples
⊩ Node.js Examples
⊩ Node.js - Handle Get Requests
⊩ Node.js Example - Upload files to Node.js server
Useful Resources
⊩ Node.js Interview Questions
⊩ Node.js Interview Questions
⊩ How to Learn Programming