Delete all file in folder nodejs
How to remove all files from a directory without removing a directory itself using Node.js? Show I have found this method, which will remove files and the
directory. In that, something like I don't really know what commands should use. Thanks for the help.
asked Nov 22, 2014 at 0:58
youbetternotyoubetternot 2,3262 gold badges15 silver badges18 bronze badges 3 To remove all files from a directory, first you need to list all files in the directory using Here is an example
answered Feb 11, 2017 at 23:23
7 Yes, there is a module fs-extra. There is a method
There is also an asynchronous version of this module too. Anyone can check out the link.
answered Feb 27, 2019 at 11:03
Samiul AlamSamiul Alam 1,8943 gold badges9 silver badges27 bronze badges 3 There is package called rimraf that is very handy. It is the UNIX command rm -rf for node. Nevertheless, it can be too powerful too because you can delete folders very easily using it. The following commands will delete the files inside the folder. If you remove the *, you will remove the log folder.
https://www.npmjs.com/package/rimraf answered Mar 6, 2018 at 7:37
Nicolas GuérinetNicolas Guérinet 1,9681 gold badge26 silver badges35 bronze badges 2 It can also be done with a synchronous one-liner without dependencies:
answered Dec 16, 2021 at 22:29
JeffD23JeffD23 7,4861 gold badge27 silver badges40 bronze badges 3 Building on @Waterscroll's response, if you want to use async and await in node 8+:
answered Mar 22, 2018 at 5:15
Jason KimJason Kim 17.1k13 gold badges65 silver badges104 bronze badges 3 How about run a command line:
answered Jan 29, 2021 at 7:48
Rudy HuynhRudy Huynh 4804 silver badges10 bronze badges 1 Short vanilla Node 10 solution
answered Sep 16, 2021 at 12:04
4 Improvement on Rodrigo's answer, using async and promises
answered Aug 30, 2021 at 7:27
Promise version of directory cleanup utility function:
answered Mar 3 at 6:45
SridharKrithaSridharKritha 7,1372 gold badges43 silver badges40 bronze badges Yet another alternative:
This is actually deleting the directory entirely (with answered Jun 28 at 21:47
electrovirelectrovir 7701 gold badge16 silver badges19 bronze badges If you get this error:
Add the folder path in
answered Nov 19, 2021 at 5:21
wazwaz 1,04615 silver badges30 bronze badges This can be done with vanilla nodejs dependencies :
Adding recursive and force makes sure that non-empty folders get deleted as well. answered Sep 3 at 19:28
GroguGrogu 1,67713 silver badges24 bronze badges ❄️ You can use |