How to Delete Uploaded Files From Server in Node.js Automatically After Specified Time Using Rimraf Library Full Tutorial 2020

You are currently viewing How to Delete Uploaded Files From Server in Node.js Automatically After Specified Time Using Rimraf Library Full Tutorial 2020

 

 

 

Get Started

 

 

In order to get started you need to install a node.js dependency called as rimraf which lets you delete files stored on the server after a specified time in milliseconds. So it will automatically delete the older files for you.

 

 

 

Command Used

 

 

npm i rimraf

 

 

 

Usage

 

 

In order to use this simply make the index.js file and copy paste the following below code

 

 

 

 

 

 

Here first of all we are including the required dependency rimraf and secondly we also need the built in fs dependency to interact with the file system.

 

After this inside the code snippet you can provide the time in milliseconds after which it will automatically delete the files which are older than the specified time.

 

And lastly we need to provide the directory where it will constantly monitor to delete the files. Here simply provide the absolute path of the directory where you will be storing your files on the server.

 

 

 

 

Leave a Reply