Working on multiple Node.js projects often leaves your system buried under countless node_modules folders. These directories can quietly eat up a surprising amount of disk space. Manually removing them is not just boring — it’s time-consuming. In this post, we’ll explore fast, efficient ways to delete all node_modules folders recursively and free up your system.
💡 Note: This guide uses Unix-based commands and is intended for macOS and Linux. Windows users can follow along using WSL or Git Bash. npkill works on all platforms.
Basic Command to Delete node_modules
The simplest way to delete all node_modules directories from the current folder and its subdirectories is by using the find command:
find . -name 'node_modules' -type d -prune -exec rm -rf '{}' \
This command searches for directories named node_modules and deletes them recursively. However, if there are many directories to delete, it can take a long time.
Improving the Command
To improve the efficiency of the command, you can use the + at the end of the exec command, which will combine multiple delete operations into one:
find . -name 'node_modules' -type d -prune -exec rm -rf '{}' +
While this command is faster because it batches the delete operations, it might not provide real-time feedback. It can be problematic if you want to monitor the progress or resume the operation in case it gets interrupted.
Making the Command Resumable
To make the deletion process resumable and provide real-time feedback, you can use the \; at the end of the exec command and add the -print option:
find . -name 'node_modules' -type d -prune -print -exec rm -rf '{}' \
With this command, each node_modules directory is deleted individually, and the deleted directory is printed to the console. This approach makes it easier to see the progress and resume the operation if it gets interrupted.
Usage Note
Before running any of these commands, make sure to navigate to the root directory of your projects. You can do this using the cd command. Alternatively, you can specify the project directory directly in the find command:
cd /path/to/your/project
find . -name 'node_modules' -type d -prune -print -exec rm -rf '{}' \;
Or without changing the directory:
find /path/to/your/project -name 'node_modules' -type d -prune -print -exec rm -rf '{}' \;
Interactive Method
Prefer a visual interface over terminal commands? npkill is a great tool for scanning and deleting node_modules folders interactively.
npx npkill
npkill will display a list of node_modules directories with their sizes, and you can select which ones to delete interactively.
Conclusion
Cleaning up node_modules directories is a quick win for reclaiming valuable disk space. Whether you lean toward a command-line solution or prefer something more interactive, the methods above make it easy to manage and remove these space-hungry folders. Pick the approach that fits your workflow best — and keep your development environment clean, fast, and clutter-free.
Top comments (0)