So I was trying to delete all my files inside a folder using node.

I came across 2 methods .

Method 1

Delete the folder using rmkdir. But if I plan on adding the images on the same folder then I use mkdir and creates the same folder again and appends the files to it.

Example: I have an Add Files and Delete ALL button. When I click deleteAll , the folder gets deleted. And when I click add then the folder gets created and the file gets added to that folder

Method 2

Using readdir , I loop through the files and stores in an array and then delete only the files instead of the folder.

Which is the best way to do it ? If its not among these then please advice me a better solution.

1 Answers

0
Golo Roden On Best Solutions

The rm function of ShellJS will do the trick. It works as a one-liner, and it works cross-platform, and is well tested and documented. It even supports recursive deletes.

Basically, something such as:

const { rm } = require('shelljs');

rm('-rf', '/tmp/*');

(Sample code taken from ShellJS' documentation.)