Skip to content

WebNinjaDeveloper.com

Programming Tutorials




Menu
  • Home
  • Youtube Channel
  • Official Blog
  • Nearby Places Finder
  • Direction Route Finder
  • Distance & Time Calculator
Menu

Node.js empty-trash Module to Empty Recycle Bin & Trash and Delete Specific Files in Javascript

Posted on November 5, 2022

 

 

Welcome folks today in this blog post we will be deleting specific files with specific extensions and transfer those files to the trash and recycle bin in javascript. All the full source code of the application is shown below.

 

 

 

Get Started

 

 

In order to get started you need to initialize a new node.js project by executing the below command as shown below

 

 

npm init -y

 

 

This will create the package.json file inside the project. Here we need to install the below libraries as shown below

 

 

npm i trash

 

 

This will install the above module where it will delete the files to recycle bin using the trash module

 

 

Now we first of all add the type attribute inside the package.json file and we need to provide the value module. This will help in using the import statement.

 

 

 

 

 

Now we need to copy paste the index.js file

 

 

JavaScript
1
2
3
import trash from 'trash'
 
await trash(['*.png'])

 

 

As you can see we are importing the trash module and then we are calling the trash() method and inside it we are providing the arrray of files paths and here we are using the pattern of files along side with extension. Here we are selecting all the png image files using .png extension and now if you execute the node.js app it will delete all the png image files from the root directory.

 

 

Deleting the RecycleBin Automatically

 

 

Now we need to delete all the files from the recycle bin automatically. Many times you need to manually delete files from recycle bin. For this we need to install a node.js module which is shown below

 

 

npm i empty-trash

 

 

As you can see now we need to copy paste the index.js file as shown below

 

 

JavaScript
1
2
3
import emptyTrash from 'empty-trash';
 
await emptyTrash();

 

 

As you can see we are importing the empty-trash module and then we are calling the emptyTrash() method to clear out all the files which are present inside the recycle bin.

 

 

This library also offers the cli version of the module which is shown below.

 

 

First of all we need to install the cli library globally as shown below

 

npm i -g empty-trash-cli

 

 

And now we need to execute the below command to empty the trash and recycle bin.

 

 

 

Recent Posts

  • Node.js Tutorial to Rename All Files in Directory With Custom Pattern Using fs Module
  • FFMPEG Command to Create MP4 Video Slideshow From Multiple Images in Command Line
  • How to Add Let’s Encrypt SSL Certificate to Domain Using Certbot in Nginx
  • Node.js Fluent-FFMPEG Audio Pitch & Speed Changer With Live Preview in Browser Using Express
  • React.js react-awesome-modal Example to Show Popup Modal With Animation in Browser Using Javascript
  • Angular
  • Bunjs
  • C#
  • Deno
  • django
  • Electronjs
  • java
  • javascript
  • Koajs
  • kotlin
  • Laravel
  • meteorjs
  • Nestjs
  • Nextjs
  • Nodejs
  • PHP
  • Python
  • React
  • ReactNative
  • Svelte
  • Tutorials
  • Vuejs




©2023 WebNinjaDeveloper.com | Design: Newspaperly WordPress Theme