Node.js – Delete File

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.

Steps to Delete File in Node.js

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.

Step 3: 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.

ADVERTISEMENT

Example 1 – 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 named sample.txt in Node.js.

deleteFile.js

// include node fs module
var fs = require('fs');

// delete file named 'sample.txt'
fs.unlink('sample.txt', function (err) {
	if (err) throw err;
	// if no error, file has been deleted successfully
	console.log('File deleted!');
});

Open terminal or command prompt and run this program using node command as shown below.

Output

$ node deleteFile.js
File deleted!

The file is successfully deleted.

Example 2 – 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 file named 'sample.txt' Synchronously
fs.unlinkSync('sample.txt');
console.log('File deleted!');

Open terminal or command prompt and run this program using node command as shown below.

Output

$ node deleteFileSynchronously.js
File deleted!

The file is successfully deleted.

Example 3 – 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.

deleteFile2.js

// 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!');
});

Open terminal or command prompt and run this program using node command as shown below.

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'

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.