Logo
Published on

How to remove recursively `node_modules` folders

Authors
cover

Photo by Andrea De Santis on Unsplash

How to remove recursively node_modules folders

Have you ever found yourself running out of disk space, only to discover that the node_modules folder in your projects is taking up a significant chunk of your storage? This is a common occurrence for developers working with Node.js applications, as these folders can quickly become bloated due to the dependencies and nested dependencies that they contain.

heaviest_objects_in_the_universe

Image Source Reddit

There are several effective ways to clean up your node_modules folder recursively. While preparing this post, I discovered that npm, yarn, and company have prune and cache commands. I let you check this out, as we want a recursive cleanup here.

First, run this find command to verify the folders to be deleted visually:

find . -type d -name node_modules -prune

Then, run this to delete them all:

find . -type d -name node_modules -prune -exec rm -rf {} \;

That is all folks