Projects Blog

Remove all node modules with a Shell script

In specific scenarios, there is often a need to eliminate deep folders, for example when we are managing a monorepo and we would like to clean some files in every package, as node_modules or dist. Or, simply, you want to free up disk space.

This process can be automatized implementing a Shell script, available in Unix and Linux systems (also in Git Bash).

find . -name \"node_modules\" -type d -prune -exec rm -rf '{}' +

Then, in our root package.json we can add a script:

{
"scripts": {
"clean:node_modules": "find . -name \"node_modules\" -type d -prune -exec rm -rf '{}' +",
"clean:dist": "find . -name \"dist\" -type d -prune -exec rm -rf '{}' +"
}
}

This simple Shell script allows you to clean files in your monorepo. Also, it will function in any folder, providing the flexibility to execute it wherever you choose!

Happy coding! 🚀

Related posts