Guides

npm clear cache: npm cache clean --force, npm cache verify and the npx cache

How to clear the npm cache: npm cache clean --force, npm cache verify, where ~/.npm/_cacache lives, the npx cache, and yarn and pnpm equivalents.

Published ~6 min read
On this page

Short answer: npm cache clean --force deletes npm’s package cache, the _cacache folder inside ~/.npm on Linux and macOS (%LocalAppData%\npm-cache on Windows). Run npm cache verify first: it checks the cache, drops corrupt data and keeps the rest, which is usually all a broken install needs.

Linux · macOS · Windows
npm cache verify            # check the cache, remove corrupt or unused data
npm cache clean --force     # delete the whole package cache
npm config get cache        # show where the cache lives

The behaviour below follows the npm 10 to 12 documentation of npm cache on docs.npmjs.com. The outputs come from npm 11.20.0 on Debian 12, run against a throwaway cache folder (--cache pointed at a scratch directory, offline), so no real cache was touched.

npm cache clean --force: what it deletes

Without --force, npm refuses and explains why:

Output · npm cache clean (abridged)
npm error As of npm@5, the npm cache self-heals from corruption issues by treating integrity mismatches as cache misses.
npm error As a result, data extracted from the cache is guaranteed to be valid.
npm error If you want to make sure everything is consistent, use `npm cache verify` instead.
npm error Deleting the cache can only make npm go slower, and is not likely to correct any problems you may be encountering!
...
npm error If you're sure you want to delete the entire cache, rerun this command with --force.

With --force it prints one warning, npm warn using --force Recommended protections disabled., and removes the _cacache folder. It does not touch its neighbours: in our test the npx cache (_npx) and the debug logs (_logs) were still there afterwards. The next npm install downloads everything again, so the first install after a clean is slower and needs the registry to be reachable.

To drop one package instead of everything, list the keys with npm cache ls PACKAGE and pass a key to npm cache clean KEY, which answers Deleted: KEY. That form needs no --force.

npm cache verify: the safer first step

The docs call the cache “self-healing and resistant to data corruption issues”: all data is verified against its integrity hash when it goes in and when it comes out, and a mismatch makes npm download the file again. verify runs that check over the whole cache. We appended a few bytes to a cached tarball and ran it:

Output · npm cache verify, one file corrupted
Cache verified and compressed (~/.npm/_cacache)
Content verified: 0 (0 bytes)
Corrupted content removed: 1
Content garbage-collected: 1 (186 bytes)
Missing content: 1
Index entries: 0
Finished in 0.009s

The corrupt file was removed and its index entry dropped, so npm would simply fetch that package again. A second run reported a clean cache. (npm writes your home folder as ~ in the first line; a cache elsewhere is shown with its full path.)

Where the npm cache lives, and the yarn and pnpm equivalents

ToolDefault locationShow the pathClear it
npm~/.npm/_cacache (Windows: %LocalAppData%\npm-cache)npm config get cachenpm cache clean --force
npx (npm exec)~/.npm/_npxnpm config get cachenpm cache npx rm --force (npm 11.2 or later)
Yarn 1 (Classic)A global cache in your user directoryyarn cache diryarn cache clean
Yarn 2 and laterGlobal cache in ~/.yarn/berry (default), or .yarn/cache in the projectSettings globalFolder and cacheFolderyarn cache clean --all (both caches)
pnpmThe content-addressable storepnpm store pathpnpm store prune (removes unreferenced packages)

npm config get cache prints the cache root, for example /root/.npm when you run npm as root; _cacache is the folder inside it. A cache= line in .npmrc or the npm_config_cache environment variable moves it. To see how much space it takes:

Linux · macOS
du -sh "$(npm config get cache)/_cacache"

The npm docs add that “npm will not remove data by itself: the cache will grow as new packages are installed.” On a machine that has built Node.js projects for months or years, _cacache can grow to several gigabytes. On a small VPS that is worth a look when the disk fills up; checking disk usage on Linux shows how to find the other big folders.

When clearing the cache helps, and when it does not

Clearing or verifying is worth it in these cases:

  • Freeing disk space. The only reason the npm docs give for clearing at all.
  • After a disk-full error or a killed install, run npm cache verify to throw out half-written data.
  • Testing an install from scratch. npm’s own message suggests npm install --cache /tmp/empty-cache: a fresh, temporary cache without deleting the real one.
  • A root-owned cache. After an old sudo npm install, npm stops with Your cache folder contains root-owned files, due to a bug in previous versions of npm which has since been addressed. and prints the fix itself: a sudo chown -R of the cache folder with your user and group IDs filled in. The general form is sudo chown -R $(id -u):$(id -g) "$(npm config get cache)". Deleting the cache is not needed.

EINTEGRITY errors read like sha512-... integrity checksum failed when using sha512: wanted sha512-... but got sha512-.... If the bad copy sits in the cache, npm treats it as a miss and downloads again, so an error that survives npm cache verify often means the hash in package-lock.json does not match what the registry sends now, for example after a change of registry or mirror in .npmrc, or when a private registry let someone publish the same version again. The npm docs do not cover this case, so treat it as a lead, not a rule. Check the resolved URL for that package in the lockfile before you delete anything.

Clearing does not help with dependency conflicts (ERESOLVE), a missing package (E404), the wrong Node.js version, permission errors on global installs, or an npm that the shell cannot find, which npm: command not found covers. For a broken node_modules, deleting that folder and running npm ci is the reset that matters.

Clear the npx cache

npx (npm exec) installs packages that are not in your project into a folder in the npm cache, _npx, and reuses them. When npx keeps running an old version, ask for the new one with npx PACKAGE@latest, or clear that cache. npm 11.2 added subcommands for it (npm 12 has them too); the npm 10 documentation does not list them:

npm 11.2 or later
npm cache npx ls            # one line per cached npx install
npm cache npx rm --force    # remove all of them
rm -rf ~/.npm/_npx          # the same by hand, also on npm 10

In our test, npm cache npx ls printed one hash per install followed by the package it holds, npm cache npx rm without --force stopped with Please use --force to remove entire npx cache, and afterwards npm cache npx ls answered npx cache does not exist. If npx itself is missing, see npx: command not found.

npm cache in Docker images and CI

In a Dockerfile, the cache that npm ci fills ends up in the image layer. If you want it gone, clean in the same RUN line, because a later RUN cannot make an earlier layer smaller:

Dockerfile
RUN npm ci --omit=dev && npm cache clean --force

With BuildKit, a cache mount is the alternative that Docker’s build cache docs show for npm: RUN --mount=type=cache,target=/root/.npm npm ci --omit=dev keeps the npm cache in a mount that is reused across builds instead of in an image layer (the target is the cache folder of the user the build runs as).

On a VPS that builds Node.js apps, a monthly npm cache verify or a du check is enough; clean with --force when you need the space back, not as a cure for every failed install. If a fresh clone fails because a tool inside node_modules cannot be found, vite: command not found covers that case.

Frequently asked

How do I clear the npm cache?

Run npm cache clean --force. It deletes the _cacache folder inside npm's cache directory, which is ~/.npm on Linux and macOS and %LocalAppData%\npm-cache on Windows. Without --force npm refuses and suggests npm cache verify, which checks the cache and removes only corrupt or unused data.

Is it safe to delete the npm cache?

Yes. The cache only holds copies of downloaded packages and registry data, so npm downloads them again on the next install. That install is slower and needs the registry to be reachable, and clearing rarely fixes a failed install because npm already refetches corrupt cache entries by itself.

Does npm cache clean also clear the npx cache?

No. The clean command removes only the _cacache folder; packages that npx installed stay in the _npx folder next to it. Remove those with npm cache npx rm --force on npm 11.2 or later, or delete ~/.npm/_npx by hand on older versions.

Where is the npm cache located?

Run npm config get cache. The default is ~/.npm on Linux and macOS and %LocalAppData%\npm-cache on Windows; the package data sits in its _cacache subfolder and npx installs in _npx. A cache= line in .npmrc or the npm_config_cache environment variable moves it.

Related articles

All guides