DevCleaner
Menu
Skip to content
Browse guides

How to Remove node_modules Safely (and Reclaim GBs)

rm -rf node_modules deletes that folder and everything in it. Reinstall from the lockfile afterward. Do not do it when the lockfile is missing or a dev server is still running.

On this page

What remove node_modules does

node_modules is the folder npm, Yarn, or pnpm fills when it installs dependencies. It is not your source. package.json and the lockfile (package-lock.json, yarn.lock, or pnpm-lock.yaml) are what you keep in git. The folder is the result of an install.

To remove node_modules, delete node_modules, or rm node_modules, the command is:

rm -rf node_modules

Run it from the project directory. rm -rf removes the node_modules directory and everything inside it, and it does not ask first. It does not remove package.json, the lockfile, or your source, because those files are not inside that directory. If you pass a different path, it deletes that path instead. After the command, the project cannot import its dependencies until you install again.

Reinstall afterwards

For npm, npm ci is the clean install from the lockfile:

rm -rf node_modules
npm ci

npm's documentation says npm ci requires an existing package-lock.json or npm-shrinkwrap.json. If the lock does not match package.json, npm ci exits with an error instead of updating the lock. If node_modules is already present, npm ci removes it before installing. It does not write package.json or the lockfile.

With Yarn's lockfile, the matching install is yarn install --immutable. With pnpm's lockfile, it is pnpm install --frozen-lockfile. If the flag is rejected, check that tool's help for the version you have installed. Install from the lockfile you already have, rather than resolving version ranges again.

When not to remove node_modules

  • The lockfile is missing. rm -rf node_modules still deletes the folder, but npm ci will not restore it. npm install resolves the ranges in package.json and can install different versions than the tree you deleted. Restore or create a lockfile first if you need the same dependencies.
  • A dev server or build is running. Stop it first. Removing the folder while a process is reading it will fail that process.
  • You changed files inside node_modules, or you depend on a link or a local patch that install will not recreate. Copy those changes out first. A lockfile is not a backup of edits inside the folder.
  • A native module is still compiling. Let that build finish or fail before you delete the tree.

Keep what cannot be reinstalled

Save local dependency patches and the source of any linked package. Confirm you can still reach private registries, and keep packages you cannot download again. DevCleaner lists project node_modules as Warning: a reinstall is a deliberate cost, not an automatic Safe cleanup.

Find node_modules folders

Each project has its own folder. Abandoned checkouts keep theirs until you remove them. From the directory where you keep code:

find ~/Projects -name node_modules -type d -prune -exec du -sh {} \; 2>/dev/null | sort -hr

Change ~/Projects to your own directory. -prune stops the search from walking into nested node_modules inside dependencies. du prints the size of each folder it finds. Read that number on your machine.

npkill lists these folders and deletes the one you choose. Check the scan root it shows before you delete anything.

What DevCleaner lists

DevCleaner's Project Artifacts scanner walks the project roots you configured and lists node_modules in those projects, including projects you still work on. Rows are grouped by project. A running build is skipped, and so are Git-tracked files. Finding them is free. On a new Free installation, cleaning them requires Pro. Project Auto Clean is a Pro setting and is off by default. It can include node_modules when Include dependencies is on. On macOS 1.17.0 and later, Settings, General, Deletion defaults to By risk: Safe items are deleted permanently, and Warning and Danger items move to the Trash. A selected node_modules row is Warning, so By risk moves it to the Trash. Empty the Trash when you want that space back.

Not the same as clearing the npm, Yarn, or pnpm cache

Deleting node_modules removes the install inside that project. It does not clear the download caches those tools keep for the next install. See the npm, Yarn, and pnpm cache guides for those folders and commands.

FAQ

What does rm -rf node_modules do?
It deletes the node_modules directory in the current folder and everything inside it. It does not delete package.json, the lockfile, or your source. Reinstall from the lockfile afterward.
Does deleting node_modules break git?
No, when node_modules is listed in .gitignore and was not committed. The command only removes that directory.
When should I not delete node_modules?
When the lockfile is missing, a dev server or build is running, or the folder holds a local change that install will not recreate.
Is remove node_modules the same as clearing the npm cache?
No. The cache is a separate folder. Deleting node_modules does not clear it.

List node_modules from your project roots

DevCleaner lists node_modules under the project roots you set, each sized and labelled Warning. Cleaning those rows requires Pro on a new Free installation. It can also clean npm, Yarn, and pnpm caches, which are a separate scan. You see the selection before anything is deleted. On macOS 1.17.0 and later the default deletion mode is By risk, so a Warning folder moves to the Trash.

Download DevCleaner: free ↓
Safe cleanup free · macOS 14+
Developer ID signed · notarized by Apple

References and scope

Reviewed on November 3, 2026 against npm's npm ci documentation and DevCleaner 1.17.1 (supported tools, settings, and staying safe). This page does not quote a size for node_modules. Measure the folder on your machine.

Featured on TryMacApps Featured on UsefulShelf Featured on Formac Listed on MacTools Pro Fazier badge