Use npkill to find and delete old node_modules folders

By

Updated

Tagged: Node.js

Find old node_modules folders and build caches with npkill, sort them by size or age, and delete the folders you pick.

npkill is a free terminal tool for finding old node_modules folders and other development leftovers. It shows each folder’s size and when you last changed the project. You can delete the folders you pick with one key.

I ran npkill 0.12.2 in my ~/dev folder in October 2026. In about 13 seconds it found 128 node_modules folders, 58.68 GB in total. Over two runs I deleted all of them except the 9 that belong to projects I’m working on, and npkill reported 49.89 GB of space released.

If you have Node.js on your computer, open a terminal in the folder that holds your projects. Run this to look for node_modules:

npx npkill@latest -t node_modules

npkill is open source under the MIT license, made by Nya García Gallardo and Juan Torres Gómez. It works on macOS, Linux and Windows.

Why node_modules folders get so big

Every JavaScript project keeps its dependencies in its own node_modules folder. npm gives each project its own copy of every package, so if ten projects use React, you have ten copies of React on your disk.

A fresh Vite and React app starts at about 53 MB, as I measured in my post about the size of node_modules. Real projects add a router and a database client, and the folder keeps growing.

Some tools make it much bigger. My biggest folder belongs to the Remotion project I use to render the intro videos for my posts, and it’s 7.6 GB. Of that, 7.3 GB is build cache. Remotion bundles the code of a video with webpack, and webpack keeps its cache in node_modules/.cache/webpack.

The real problem is old projects. You finish something, you move on, and its node_modules folder stays on your disk forever. Deleting it is safe, because npm install rebuilds it from package.json and the lockfile when you come back to the project.

Fig 1 · 1/4What deleting node_modules removes
Project before cleanup

How to run npkill

npx downloads the package and runs it. If it needs to download npkill, it asks you to confirm first.

npx npkill@latest

If you’d rather have the npkill command always available, install it globally:

npm install -g npkill

By default, npkill 1.0 scans from the current directory using its built-in node profile. That finds node_modules as well as folders like .next, .nuxt, and .turbo. The profile checks for a nearby package.json before reporting a .next folder.

Use -d to start somewhere else:

npx npkill -d ~/dev

The interactive interface needs a terminal window. If you’re running a script, use JSON output.

Thanks to npkill co-creator Juan Torres Gómez for pointing out the changes in the 1.0 release.

Reading the list

npkill adds folders to the list as it finds them, then calculates two numbers for each one.

Size is the space the folder takes. npkill picks MB or GB automatically. Add --size-unit gb to always see gigabytes.

Age estimates when you last changed a file in the project. npkill skips folders like node_modules when calculating it. A value of 772d means the newest change it found was more than two years ago.

At the top, Releasable space adds up the size of the results, and Space saved shows how much you’ve freed so far.

You do everything with the keyboard:

A deleted folder gets a [DELETED] label in the list. npkill deletes it directly, bypassing the Trash. For a project’s node_modules, you can run npm install to rebuild it.

When I quit after my second cleanup, npkill printed how much I’d freed:

Space released: 40.72 GB
Thanks for using npkill!

Sort by age to find old projects

The list comes in the order npkill finds the folders. Sort it with -s. This puts the projects you haven’t touched in the longest time at the top:

npx npkill -s age

This puts the biggest folders at the top:

npx npkill -s size

You can also sort with -s path, which is alphabetical.

How I use npkill

The first time I used npkill was in December 2022, to clean up ~/www/old, the folder where my old website projects end up.

In October 2026 I ran it twice in ~/dev, where I keep my Mac apps, CLI tools and experiments.

In the first run, with a plain npx npkill, I deleted the node_modules of six showreel projects. Those are the Remotion projects that render the 30-second videos for my app launches, and each one had between 0.78 and 2.11 GB of node_modules. The six together freed 9.16 GB.

Then I ran it again sorted by age, in gigabytes. In npkill 1.0, this is the equivalent command:

npx npkill -t node_modules --size-unit gb -s age

The top of the list was boilerplate projects and example apps from past Bootcamp editions, untouched for 500 to 770 days. I went down the list and deleted every folder except the ones I’m working on now. That was 113 more folders and 40.72 GB.

I kept 9 folders. The biggest is the 7.6 GB node_modules of the Remotion project that renders my post intro videos. I still use that project, and its build cache saves time on every render.

Each scan of ~/dev took about 13 seconds.

The folders marked with ⚠️

Some node_modules folders belong to apps, not to your projects. Visual Studio Code and Cursor extensions ship with their own node_modules, and so do some desktop apps built with Electron. Delete one of those and the extension or the app breaks until you reinstall it.

npkill uses the path to flag folders that may be sensitive. It hides those results by default. Use --include-sensitive to show them with a ⚠️.

On my Mac, ~/.cursor/extensions has 11 node_modules folders and ~/.vscode/extensions has 19. Leave those alone.

~/.npm/_npx has 115. That’s the npx cache, where npkill itself ends up after you run it with npx. Those are safe to delete, because npx downloads a package again the next time you run it.

Use -f to scan from your home folder with the default filtering:

npx npkill -f -t node_modules

Search for other folders

-t replaces the default profile with the folder names you specify. For example, you can look for node_modules and Next.js build output in the same scan:

npx npkill -t node_modules,.next

dist and build hold build output in many JavaScript projects, vendor holds the dependencies of a PHP project installed with Composer, and target is where Rust puts its builds. Be careful with generic names, because some projects keep scripts in a folder called build, and npkill can’t tell the difference.

For other ecosystems, run npx npkill --profiles to see the built-in profiles. For example, --profiles node,python scans for both Node.js and Python folders.

Exclude folders

-E skips folders. Put the names inside quotes, separated by commas:

npx npkill -E "clients, archive"

npkill matches whole directory names. -E old skips directories named old at any depth. Use -E ./old to skip just the old directory directly inside the scan root.

npkill skips some directories by default, including .git. Use --no-ignore-default if you need to scan inside them.

Delete everything at once

-D (short for --delete-all) deletes every match. It requires explicit targets with -t or --targets. If you want to clear node_modules out of a folder full of backups, name that target and preview the cleanup first.

--dry-run simulates deletion. Sensitive results stay hidden by default.

npx npkill -d ~/backups -t node_modules -D --dry-run

When the list looks right, run the same command without --dry-run. npkill asks you to confirm before it starts unless you pass -y. Press q when it’s done.

Preview a bulk cleanupChoose a directory and explicit target, then review the folders found by a dry run before deleting them.YesNoChoose scandirectorySet the target with-t node_modulesRun -D with--dry-runInspect selectedfoldersSelection iscorrect?Repeat without--dry-runAdjust exclusionsand preview again

Before you delete

If the project has a lockfile (package-lock.json, pnpm-lock.yaml or bun.lock), the next install gets the exact versions you had. If it doesn’t, npm picks the versions again from the ranges in package.json, and you may get newer ones. I explain how the lockfile works in this lesson of my free Node.js course.

Build caches stored in node_modules/.cache, like webpack’s, go away with the folder, so the first build after reinstalling is slower.

The size npkill shows can be bigger than the space you get back. pnpm keeps one copy of package files in a global store (on my Mac that’s ~/Library/pnpm/store/v10) and hard links them into node_modules. Bun on macOS does something similar with copy-on-write clones of its cache. When you delete those folders, the files are still in the store, so you free much less than npkill reports.

To clean the store, run pnpm store prune, which removes the packages no project uses, or bun pm cache rm, which clears Bun’s cache. I explain how pnpm and Bun share packages in Managing node_modules efficiently with pnpm and Bun. Most of the projects I cleaned used npm, so most of my 49.89 GB was real space.

Can a coding agent run npkill?

Yes. Use --json to list folders and their sizes without opening the interactive interface:

npx npkill -d ~/dev -t node_modules --json

Have the agent show you the results so you can pick what to delete. You can also list the folders with find and du:

find ~/dev -name node_modules -type d -prune -exec du -sh '{}' +

It’s the same command from my post on removing all the node_modules folders, with du -sh in place of rm -rf. That post’s one-liner is also the alternative to npkill when you want to delete every node_modules under a folder and don’t need to see the list first.

Want me to talk about your product? You can sponsor this site.

Related posts about node: