0

How to Delete a File From Folder in Node JS

The following tutorial will portray how to delete file that you do not require anymore, deleting files using node js greatly easy with node js’ fs library.

Note: There is no way of undo deleted files, just keep that in mind.

fs.unlink callback, fs.unlink multiple files, fs.unlink not working, fs.unlink wildcard, fs.unlinksync error, multer delete file, node fs unlink, nodejs delete file

To delete documents, just pass a file path as parameter to fs.unlink(path, callback), for sync version call unlinkSync(path). Take note of that unlink bargains just with files, to delete directories you require emphasize over directories and files, will show you erasing directories and it’s records likewise here. it’s too simple, would it say it isn’t?

How to use unlink() to Delete a file

Now to delete testFile.txt, we simply run a node js script which is located in the same directory where file is located. Unlink method just needs to know the name of the file, if file is in another location pass the path of the file, by default it will search for file in the script location(current directory of the script file)

asynchronous – delete example

synchronous – delete example

Delete files of directory

So our first task is to iterate over directories and sub directories to find the files, after finding file just apply unlink() method, lets do it… –

Delete array of files

Just iterate over the array of files and apply unlink() method on each file as shown below.

Leave a Reply

Your email address will not be published. Required fields are marked *