npm made cache clean refuse to run without --force on purpose. Since npm 5 the cache verifies content by hash and repairs corruption automatically, so clearing it is not a fix for broken installs — it just makes the next install slower. The flag exists to make you confirm you meant it.
This command has become folk medicine. Something goes wrong with an install, someone suggests clearing the cache, it appears to help, and the advice propagates.
What usually happened is that the same instructions also said to delete node_modules and the lockfile, and that is the part that fixed it. Knowing which half did the work saves you re-downloading your entire dependency tree every time something breaks.
Table of contents
- Why the flag is required
- What to run instead when installs are broken
- When clearing the cache is genuinely right
- The CI case, where this advice inverts
- How this fits the rest of the stack
- FAQ
Why the flag is required
Running it without the flag gives you a refusal rather than a prompt:
npm ERR! As of npm@5, the npm cache self-heals from corruption issues
npm ERR! and data extracted from the cache is guaranteed to be valid.
npm ERR! If you want to make sure everything is consistent, use `npm cache verify`.
That message is the documentation. npm 5 replaced the old cache with cacache, which is content-addressable: every entry is stored under the hash of its content, and every read verifies the hash matches. A corrupted entry fails its check and is re-fetched automatically.
So the failure mode that cache clean used to fix — a truncated or corrupted tarball being served forever — cannot persist any more. The command was kept for reclaiming disk space, and the flag was added because people kept reaching for it reflexively.
The command npm actually wants you to run:
npm cache verify
That checks the index, garbage-collects unreferenced content, and reports what it found — without discarding everything.
What to run instead when installs are broken
In roughly the order they are worth trying:
- Delete node_modules and the lockfile. This is the step that actually resolves most install problems, because the bad state is in the resolved tree rather than the cache.
- Check your Node and npm versions. A lockfile written by a much newer npm can behave oddly on an older one.
- Run npm cache verify. Cheap, non-destructive, and reports genuine index problems.
- Then, if you still suspect the cache, clear it.
rm -rf node_modules package-lock.json
npm install
On Windows PowerShell that is Remove-Item -Recurse -Force node_modules, package-lock.json.
One caveat: deleting package-lock.json means the new install resolves fresh versions within your semver ranges, so you can pick up updates you did not intend. In CI, or when you want the exact recorded tree, delete only node_modules and use npm ci:
rm -rf node_modules
npm ci
npm ci installs strictly from the lockfile and fails if the lockfile and package.json disagree — which is usually the more informative outcome.
When clearing the cache is genuinely right
There are real cases, they are just narrower than the folklore suggests.
- Disk space. The cache grows without bound on a long-lived machine or a build agent.
npm cache clean --forcereclaims it, and that is the documented purpose. - A republished package under the same version. Rare and against npm policy, but it happens with private registries. The cache has the old content under a version that now means something else.
- Switching registries. Moving between a private registry and the public one can leave entries that resolve confusingly.
- Credential changes for private packages. A cached failure response can outlive the auth problem that produced it.
Check the size before deciding it is worth it:
npm cache ls 2>/dev/null | wc -l
du -sh $(npm config get cache)
A multi-gigabyte cache on a build agent is a real reason to clear it. A 200MB cache on your laptop is not the cause of your failing install.
The CI case, where this advice inverts
On a build agent, the cache is doing useful work and clearing it is actively harmful — you are throwing away the thing that makes builds fast.
If a CI build fails in a way that a cache clear appears to fix, the real problem is almost always that the cache key is wrong. A cache keyed on something that does not change when your dependencies change will serve a stale tree indefinitely:
- uses: actions/cache@v4
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
Keying on a hash of the lockfile means the cache is reused when dependencies are unchanged and invalidated the moment they change. That is the correct behaviour, and it removes the temptation to clear caches as a ritual.
Use npm ci rather than npm install in CI as well. It is faster, it installs exactly what the lockfile records, and it fails loudly on a mismatch instead of quietly resolving something different from what you tested locally.
How this fits the rest of the stack
The pattern worth taking from this: when a fix works but you cannot explain why, you usually applied two changes and credited the wrong one. Clearing the npm cache is the classic example — it rides along with deleting node_modules, and takes the credit.
The same reasoning applies to builds generally. A build you cannot explain is a build that will surprise you, and the defence is making each run reproducible and its output inspectable. RunxBuild builds services and static sites from your repository with a log for every deploy, so what was installed and what failed are visible per build rather than inferred. If you are working out what the running result costs alongside its database and storage, the RunxBuild hosting calculator itemises them.
Useful related references:
- psql Drop Database: DROP DATABASE, FORCE, and the Active-Session Trap
- npm Clean Install: When Deleting node_modules Is Right, When
npm ciIs Right, and When Both Are Slowing You Down - WordPress Malware Removal: Clean It Properly or Do It Again Next Week
- Services on RunxBuild
FAQ
Why does npm cache clean require —force?
Since npm 5 the cache is content-addressable and verifies every entry by hash, so corrupted data is detected and re-fetched automatically. Clearing it is not a fix for broken installs, and the flag exists to make you confirm you really want to discard it. npm suggests npm cache verify instead.
Will clearing the npm cache fix a failed install?
Rarely. Most install problems live in node_modules or the lockfile, not the cache. Delete node_modules and package-lock.json and reinstall — that is the step doing the work when the combined advice appears to help.
What is the difference between npm cache clean and npm cache verify?
clean deletes the entire cache, so everything is downloaded again next time. verify checks the index for consistency, garbage-collects unreferenced content and reports what it found, without discarding valid entries. Verify is almost always the one you want.
Where is the npm cache stored?
Run npm config get cache to see the path. It defaults to ~/.npm/_cacache on Linux and macOS, and %AppData%/npm-cache on Windows. Check its size with du -sh before deciding a clear is worthwhile.
Should I clear the npm cache in CI?
No — the cache is what makes CI installs fast. If clearing it appears to fix a build, the actual problem is usually a cache key that does not change when your dependencies do. Key the cache on a hash of package-lock.json and use npm ci rather than npm install.