npm Says 0-Exit, Disk Says 370M: The Hidden .name-XXXXXXXX Trash That Survives prune
Anyone using managed CI containers, sandboxes, or security-instrumented Node runtimes can silently accumulate stale dependencies and disk bloat despite npm's clean exit codes. The failure mode is environment-specific, so scripts that work locally may quietly fail in CI, and knowing where the delete path diverges is the difference between a 2-second false success and a real cleanup.
A managed CI cleanup with `npm prune` exited 0 and printed "up to date," yet `du -sh node_modules` still showed 370M. The culprit: npm's two-phase delete first atomically renames the target directory, then recursively deletes it. A safe-delete shim in the environment blocks the second phase when a batch exceeds ~50 targets, so directories get renamed to hidden `.pkg-XXXXXXXX` names and then abandoned. npm treats the block as a non-error and returns success, creating three stacked illusions that the deletion worked.
The problem hides from ordinary checks because `ls` without `-a` doesn't show the hidden directories, and scoped `@scope/` directories are often skipped entirely by `prune`. In the reported case, 84 hidden trash directories plus a 217M `@tencent-ai` scoped folder remained. The fix relies on explicit `du` and hidden-directory checks, manual deletion with Git Bash `rm.exe` (which bypasses the shim), and cross-referencing `package-lock.json` to identify removable residue. After applying the steps, the node_modules size dropped from 370M to 80M.
The silent failure stems from npm's exit code reflecting only the rename phase, not the recursive delete; an exit 0 is a contract about the operation's start, not its completion.
Deletion shims that guard against mass deletions create a blind spot for node_modules, where huge scoped directories routinely survive prune and get misattributed to permission or path problems.
Environment-specific behavior like Git Bash rm bypassing the shim means the same command works or fails depending on the shell, which makes debugging far harder because the toolchain, not the logic, is the variable.
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)