banjocode Clear All Node Modules Folders Recursively

Clear All Node Modules Folders Recursively

Remove all node_modules folders within a directory with one simple command.

1 min read

Clearing node_modules

The node_modules folder takes up a lot of space, especially if you have several projects in a folder. This simple bash command will help you clear all node_module folders within the directory you are located in.

find . -name "node_modules" -type d -prune -exec rm -rf '{}' +
  • find . - find in the current directory
  • -name "node_modules" - with the correct name
  • -type d - only directories
  • -prune - instructs find command not to descend into the current directory for it to exclude child node_modules directories.
  • -exec rm -rf '{}' + - execute rm -rf on the matching result. The '{}' + will append each selected file name at the end, improving performance.