跪拜 Guibai
← Back to the summary

npm Says 0-Exit, Disk Says 370M: The Hidden .name-XXXXXXXX Trash That Survives prune

① Scenario: Command exits 0, but disk usage doesn't drop a single byte

Once, while cleaning project dependencies in a company-managed CI environment, I ran a very common command:

npm prune

The output was clean:

up to date in 2s

The exit code was 0; everything looked as smooth as possible. I then glanced at the top-level directory:

ls node_modules

The originally dense dependencies had indeed shrunk by over eighty, looking as if they'd been "properly deleted."

But when I habitually checked disk usage, I was stunned:

du -sh node_modules

It was still 370M, exactly the same as before cleanup. Files were visibly fewer, but space hadn't moved at all. The contradiction made me think I was looking at the wrong directory.

Only when I added a -a flag:

ls -a node_modules

Did the truth surface: dozens of hidden directories named .packageName-XXXXXXXX, each followed by an 8-character random string, lay in the directory. The packages hadn't been deleted; they'd been "disguised" and hidden.

This phenomenon is especially likely to appear in sandboxes with safe-delete batch-deletion protection, CI containers, and Node runtimes injected with deletion-interception shims. If you've encountered the oddity of "npm says deleted, but disk doesn't shrink," this article gives you a directly copyable diagnosis and fix path.

② Root cause: npm's deletion is two-phase; protection blocks the second phase

When npm deletes a package, it's not a direct rm -rf underneath. It first performs an atomic rename of the target directory to a temporary name within the same directory; then recursively deletes the temporary directory. This two-phase design is meant to make the deletion "logically effective" quickly, then slowly clear physical files.

The problem lies in the second step.

Managed sandboxes or CI environments usually attach a safe-delete shim that intercepts batch recursive deletion, with a protection threshold typically around more than 50 targets per round. When you uninstall a large number of dependencies at once, the second-phase recursive deletion hits this threshold:

  1. The rename step succeeds; ls node_modules no longer shows those dependencies at the top level—Illusion one: looks deleted
  2. The recursive deletion is blocked; packages and their contents lie intact in hidden directories .xxx-XXXXXXXX—Illusion two: npm reports success
  3. npm doesn't treat the interception as an error; it continues to output up to date and returns 0—Illusion three: nothing happened

Stacked together, the three illusions produce that counterintuitive result: npm says it cleaned everything, but the disk stays still.

This investigation also exposed an extra pitfall: npm prune's cleanup scope is itself incomplete. Ordinary packages go through the renamed-and-hidden process above, but @scope/ scoped directories are often left entirely intact. In my project then, @tencent-ai took up a full 217M, and after running npm prune it remained firmly in place, requiring manual supplementary deletion.

③ Why it's so hard to find: the illusions are all "legitimate"

The trickiest part of this problem is that each step looks "reasonable."

npm's exit code is 0, output says up to date, and the top-level ls really does show fewer directories. If you rely only on tool status codes and routine directory checks, you'll almost never discover the issue. Only when you compare the physical fact of "disk usage" do the contradictions emerge.

Meanwhile, hidden directories don't appear under a normal ls; you must explicitly add -a. Many people habitually glance at the top-level directory and assume dependencies are gone, so this kind of residue can stay in node_modules until the disk is nearly stuffed and then suddenly explodes.

There's also a toolchain mismatch: Git Bash's rm in the terminal doesn't go through the safe-delete shim, so manual deletion succeeds; but fs.rm in Node scripts goes through the shim and may be blocked. Many people can't delete via scripts but can manually, leading them to suspect permission or path issues and go off in completely the wrong direction.

④ Quick diagnosis: three commands to locate hidden trash

From now on, after uninstalling dependencies in a managed environment, don't rush to merge code. Do these three steps first:

First, verify whether disk space actually dropped:

du -sh node_modules

If you expected to free several hundred MB but the result is almost unchanged, you can basically conclude there's residue.

Second, count hidden trash directories:

ls -a node_modules | grep -E '^\.-[^-]*-[A-Za-z0-9]{8}$' | wc -l

This regex matches the .name-XXXXXXXX structure: starts with ., has a - after the leading part, followed by an 8-character random string. If the output isn't 0, you've hit the issue.

Third, check for leftover scoped directories:

ls -d node_modules/@* 2>/dev/null

npm prune often misses @scope/ directories; this step helps you catch large missed directories.

In my case, the first command showed 370M → 370M, the second counted 84 hidden trash directories, and the third caught the uncleaned @tencent-ai at 217M. With three facts combined, the root cause was confirmed.

⑤ Fix and avoidance: don't expect npm to clean it all in one go

In a managed environment with deletion protection, the strategy for uninstalling dependencies must proactively change to "declare first, then delete manually."

One command to clear all trash residue while keeping .bin and .package-lock.json:

cd node_modules || exit 1
for f in $(ls -a | grep -E '^\.' | grep -vE '^(\.|\.\.|\.bin|\.package-lock\.json)$'); do
  rm -rf -- "./$f"
done

A key detail here: use Git Bash / MSYS's rm.exe, not Node's fs.rm. The former doesn't go through the safe-delete shim and can actually delete directories; the latter, once intercepted, produces another "script doesn't delete cleanly" anomaly.

For scoped directories, it's recommended to manually remove them:

rm -rf node_modules/@tencent-ai

Deciding which directories are safe to delete—don't guess: open package-lock.json and check the node_modules/* entry list. Anything not in the lock file is residue and can be deleted directly; those in the lock must be kept. I used this to confirm that @derhuerst (a dependency of ffmpeg-static) had to stay, while 8 scoped directories like @tencent-ai could be removed.

A better uninstall order, proven in practice:

  1. Edit package.json to remove the dependency lines you want to uninstall
  2. Run npm install --ignore-scripts to let the lock file converge correctly
  3. Manually rm -rf large directories, especially scoped directories
  4. Manually clear the .xxx-XXXXXXXX hidden trash
  5. Run npm install --ignore-scripts again to restore small packages still recorded in the lock

After deletion, do a real functional check—don't just look at whether directories exist:

node -e "const p=require('ffmpeg-static'); console.log(p, require('fs').existsSync(p))"

After these operations, the 84 trash directories were cleared, node_modules dropped from 370M to 80M, and the ffmpeg-static binary validation passed.

⑥ Acceptance criteria

❌ Fail

✅ Pass


If you've also encountered a silent failure like "command reports success but the result is wrong," feel free to share the specific symptom in the comments, especially weird cases in sandboxed, CI, or security-controlled Node environments. Together we can debug faster.

Comments

Top 1 of 2 from juejin.cn, machine-translated. The original thread is authoritative.

去吧波波鸟

Exit code 0 can still be fake, this is infuriating. The ls -a trick does work, those hidden 84 directories are exposed at a glance. From now on, after deleting dependencies, I'll first check with du -sh to compare numbers, that's more reliable than looking at the output.

云屿智研

I rely entirely on AI now, feeling like I'm getting dumber, whatever happens AI solves it. (facepalm)