[Note] Wiping every node_modules folder to free up disk space

Anyone doing Node/JS/.NET dev already knows the node_modules folder is the black hole of the universe. Even a tiny demo project eats 50MB-200MB. Multiply that by a few dozen old projects you haven’t touched in a year (POCs, tutorials, side projects…) and there go a few dozen GB of your drive.

The cleanup solution is very simple: Nuke everything. Whenever you need to work on a project again, go in and npm install later.

Noting down this script so I can run a drive clean once in a while.

1. Dry Run (Check the size first)

You should run this first to check that the command finds the right files and to see how much disk space you’re wasting.

Move into the root folder that holds your source code (e.g. cd ~/Projects or cd D:\DevWork), then run:

Mac / Linux:

Find all node_modules folders and total up their size:

1
find . -name "node_modules" -type d -prune -print | xargs du -chs

Windows:

1
FOR /d /r . %d in (node_modules) DO @IF EXIST "%d" echo %d

2. Execution (Wipe them out)

Once you’ve confirmed the list from step 1 looks good, run this command to delete.
Warning: Once this runs, everything is gone permanently, no undo. Check carefully which folder you’re standing in (pwd) before hitting Enter.

Mac / Linux:

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

Windows:

If you’re using cmder or the git bash cli

1
FOR /d /r . %d in (node_modules) DO @IF EXIST "%d" rm -rf "%d"

Windows (CMD):
Note: This command uses rm -rf (if you have Git Bash/Tools installed) or you have to change it to rd /s /q if you’re on plain CMD.

1
FOR /d /r . %d in (node_modules) DO @IF EXIST "%d" rd /s /q "%d"

After running it, the machine feels noticeably lighter.

 Comments
Comment plugin failed to load
Loading comment plugin