How to Delete File If Exists in Node JS?

By Hardik Savani September 15, 2021 Category : Node JS

This example is focused on node js delete file if exists. if you want to see example of how to delete file if exists in node js then you are a right place. This tutorial will give you simple example of node js remove file if exists. In this article, we will implement a node js delete file sync. Let's see bellow example node js unlinkSync delete file.

We will use fs npm package for remove file in node.js. fs package provide unlinkSync() for remove file. let's see simple example

Step 1: Create Node App

run bellow command and create node app.

mkdir my-app

cd my-app

npm init

Step 2: Create server.js file

Make sure you have "uploads" folder in root directory with some files on that.


const fs = require('fs');

const filePath = './uploads/image-1616507041795.png';

fs.exists(filePath, function(exists) {

if(exists) {

console.log('File exists. Deleting now ...');


} else {

console.log('File not found, so not deleting.');



now you can simply run by following command:

node server.js


i hope it can help you...

Tags :