SpaceFreeSpaceFree

How to delete node_modules on Windows, and get the space back with pnpm

By Bhavik · Sources checked October 3, 2026

Every JavaScript project gets its own node_modules folder. npm puts packages in ./node_modules at the project root, and each package's own dependencies get unpacked into a nested node_modules inside it (npm folders). If you have a lot of projects, that adds up fast.

Is it safe to delete?

For your own projects, yes. The folder can always be rebuilt from package.json and your lock file. npm ci even deletes an existing node_modules before it installs, and it needs a package-lock.json or npm-shrinkwrap.json to run (npm ci). Check these first:

  • The project has a lock file (package-lock.json, pnpm-lock.yaml or yarn.lock). Without one, a reinstall can pull different versions.
  • You will have internet, or a package cache, when you next open the project.
  • It is your own project, not an app installed on your machine. Some installed apps ship with their own node_modules and stop working without it. npkill warns about this too.

Step 1: see how big it is

Run this from the project root in PowerShell. It adds up the size of every file in node_modules, hidden ones included (Get-ChildItem, Measure-Object). The Get-ChildItem docs say to use -LiteralPath instead of -Path when you also use -Recurse, so that is what this does.

$bytes = (Get-ChildItem -LiteralPath .\node_modules -Recurse -File -Force |
  Measure-Object -Property Length -Sum).Sum
"{0:N2} GB" -f ($bytes / 1GB)

Step 2: delete it

In Command Prompt, from the project root. /s deletes the folder and everything under it, and /q skips the confirmation prompt, so make sure you are in the right folder first (rmdir).

rmdir /s /q node_modules

In PowerShell, Remove-Item does the same job. -WhatIf shows what it would delete without deleting anything, so run that line first (Remove-Item).

Remove-Item -LiteralPath .\node_modules -Recurse -Force -WhatIf
Remove-Item -LiteralPath .\node_modules -Recurse -Force

When you open the project again, run npm ci, npm install or pnpm install and the folder comes back.

Clearing out old projects in one go

Going project by project is slow if you have dozens. npkill scans a folder, lists every node_modules it finds along with its size, and lets you delete the ones you pick. -d is where to start looking, and --dry-run pretends to delete so you can try it safely (npkill). Use the arrow keys to move, Space or Del to delete, and Q to quit.

npx npkill -d C:\Users\you\projects --dry-run

Change the path to wherever your projects live, and drop --dry-run once you are happy with what it shows. It marks risky folders with a warning sign, so read those before you delete anything. It also runs through npx, which means you are downloading and running a package, so check you are comfortable with that.

pnpm: one more step

pnpm does not copy packages into every project. It keeps one copy of each package in a shared store and hard-links the files into your node_modules (pnpm motivation). That is great for disk space. It also means deleting a project's node_modules does not free the stored copy while another project still uses it. Once nothing references a package any more, pnpm store prune removes it (pnpm store).

pnpm store path
pnpm store prune
pnpm says prune is harmless to your projects. The catch is that a package you pruned can be needed again later, for example when you switch to an old branch, and then it has to download again. So run it now and then, not every day (source).

Caches and WSL

node_modules is separate from the package manager's download cache. For that, see clearing npm, pip and NuGet caches. If your projects live inside WSL, the folders sit inside the distro's virtual disk. Deleting them frees space inside the disk, and Windows only gets it back after you compact it. Why your WSL2 disk file never shrinks explains how.

Doing it without the command line

SpaceFree finds node_modules folders inside your own projects, along with package caches, build output and virtual disks. It shows the size of each and tags every item as safe to delete, rebuildable or not cleanable before anything is removed. Download SpaceFree.

Sources

Find hidden disk space automatically

SpaceFree scans for WSL2 and Docker virtual disks, package caches, node_modules and build output, and shows what is safe to delete. Free for 7 days, no credit card.

Download SpaceFree for Windows

More guides