This Banner is For Sale !!
Get your ad here for a week in 20$ only and get upto 15k traffic Daily!!!

Deleting multiple directories with Nodejs/Javascript

Hiya! I needed to delete all directories that have been in a listing known as “information”, simply thought I might share the answer. ๐Ÿ˜ƒ

const { rmSync, guarantees: { readdir } } = require('fs');

(async () => {
  strive {
    const fileNames = await readdir('./information');

    for (const fileName of fileNames) {
      console.log(`deleting file with identify ${fileName}`);
      rmSync(`./information/${fileName}`, { recursive: true }); 
  } catch (error) {
    console.error('did not delete directories', error);
Enter fullscreen mode

Exit fullscreen mode

Brief and candy. ๐Ÿ˜Ž

What this principally does is learn all of the directories beneath the listing known as “information” and places them into an array of file names utilizing the readdir operate.

Then it loops by means of the names of the directories and deletes them utilizing rmSync.

Additionally it is essential to deal with errors with nodejs as an sudden error could kill the appliance. ๐Ÿฅฒ

In case you have a greater, cleaner resolution then please share. That is how I dealt with it. ๐Ÿ™‚

Like me work? I publish about a wide range of subjects, if you want to see extra please like and observe me.
Additionally I like espresso.

โ€œBuy Me A Coffeeโ€

The Article was Inspired from tech community site.
Contact us if this is inspired from your article and we will give you credit for it for serving the community.

This Banner is For Sale !!
Get your ad here for a week in 20$ only and get upto 10k Tech related traffic daily !!!

Leave a Reply

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

Want to Contribute to us or want to have 15k+ Audience read your Article ? Or Just want to make a strong Backlink?