Some times you might want to delete a whole bunch of files that exist in a file system structure. If the project you are making is aways going to be running in a POSIX environment, you could use the rm command with a child process, but say you want to make the app more portable. This is where something like rimraf may come in handy.
When testing out any kind of node.js project I often make a test_[name-of-project] folder, and test it out in there, often writing some demo scripts that make use of that project.
If you want to check out my test_rimraf on this post you can find it here.
To test out rimraf I first need a way to make some files in a path, so I made a mkfiles.js script that provides a method that helps me make a bunch of files at a given path. I then made two scripts make-basic.js, and make-junk.js that can be used to simulate the creation of a basic file structurer that contains files that I might want to keep, and files that I want gone.
In a real life situation you might have some kind of file system structure composed of an array of different file types, or maybe they follow a certain pattern in the filename. For example you might want to delete all of the *.md files in your node_modules folder, but preserve everything else that may be of use. If this is the case you can skip over this making some files section, and get on with it.
Here is the source of mkfiles if interested.
I Then used this in my make-basic script:
and also a make-junk script, that builds a more complex example structure.
So when calling my make-junk script I end up with a bunch of *.html, *.css, and *.txt files. This represents a situation in which I want to delete the *.txt files while not touching everything else. Get it? good moving on.
So this is pretty much the whole point of using rimraf, to go over the whole of a file structure and delete all files that fix a certain glob pattern.
Notice the ** glob, this will cause rimraf to search the whole structure in the source folder for text files and delete them.
You may be of a mindset where you always think to yourself “do I really need this dependency”. You may often think about how hard it might be to put together something on your own, just working with what there is to play with when it comes to the node.js core modules.
As such yes it is not to hard to get something together to do this. I was able to quickly throw together something using fs.readdir, fs.lstatSync, and fs.unlink.
Keep in mind this is something I put together in about fifteen minutes maybe. I could invest some more time, and make it a bit more robust. It could make use of promises, work well as a CLI tool, so forth, and so on. It may be a bit time consuming to get this solution to work well with respect to a wide range of use case scenarios.
rimraf works pretty well for this task. It might be nice to have something that does this that also has RegEx support, but most of the time globs get the job done just fine.
Be sure to check out my other posts on node.js, and npm packages.