Files
pnpm/.changeset/safe-clean-command.md
Zoltan Kochan 030001035b feat: add pnpm clean command for safe node_modules/lockfile removal (#10708)
* feat: add `pnpm clean` command for safe node_modules removal

Adds a new `pnpm clean` command that safely removes node_modules contents
from all workspace projects. Uses Node.js fs.rm() which correctly handles
NTFS junctions on Windows without following them into their targets,
preventing catastrophic data loss. Preserves non-pnpm hidden files
(e.g. .cache) and lockfiles by default; use --lockfile/-l to also
remove pnpm-lock.yaml files. Also cleans custom virtual-store-dir
when configured inside the project root.

Closes #10707

* fix: use is-subdir package instead of custom implementation, check existence before printing

- Replace custom isSubdir function with the existing is-subdir package
- Only print "Removing" and remove lockfile/virtualStoreDir when they exist
- Rethrow non-ENOENT errors instead of swallowing them

* refactor: use path-exists package, handle TOCTOU race in removeModulesDirContents

- Replace manual fs.access + try/catch with pathExists for cleaner existence checks
- Add ENOENT handling to removeModulesDirContents readdir to handle the race
  where the directory is removed between hasContentsToRemove and readdir

* refactor: use opts object with .bind for cleanProjectDir, rename to removeLockfile
2026-02-28 22:53:56 +01:00

6 lines
446 B
Markdown

---
"pnpm": minor
---
Added `pnpm clean` command that safely removes `node_modules` directories from all workspace projects. Unlike manual deletion with `rm -rf` or PowerShell's `Remove-Item -Recurse`, this command correctly handles NTFS junctions on Windows without following them into their targets, preventing catastrophic data loss [#10707](https://github.com/pnpm/pnpm/issues/10707). Use `--lockfile` to also remove `pnpm-lock.yaml` files.