bun prune
Remove packages that are not in bun.lock from node_modules
bun prune deletes everything in node_modules that the current bun.lock would not install — packages left behind after switching branches, removing a dependency, or installing with another package manager. With the isolated linker, this includes stale entries in node_modules/.bun.
It never contacts the registry, never runs lifecycle scripts, and never modifies bun.lock or package.json.
bun prunebun prune v1.4.0 (abc12345)
- @types/node@20.11.5
- left-pad@1.3.0
2 packages removed (checked 948) [22.00ms]Packages removed from a workspace or nested node_modules folder show the folder in parentheses, e.g. - typescript@5.4.0 (packages/app/node_modules).
--production#
Also remove everything bun install --production would not install (i.e. devDependencies). This lets you build with dev dependencies and ship without them:
COPY package.json bun.lock ./
RUN bun install --frozen-lockfile
COPY . .
RUN bun run build
RUN bun prune --production--omit=dev, --omit=optional, and --omit=peer work the same way they do for bun install.
--dry-run#
List what would be removed without deleting anything:
bun prune --production --dry-runbun prune v1.4.0 (abc12345)
- typescript@5.4.0
1 package can be removed (checked 948) [9.00ms]
bun prune --production--filter#
Prune only the selected workspaces' node_modules folders (same patterns as bun install --filter). Shared locations — the root node_modules, or node_modules/.bun with the isolated linker — are cleaned too, but anything an unselected workspace still needs is kept.
bun prune --production --filter appNotes#
- Always runs from the workspace root and covers every workspace's
node_modules, even when invoked inside a workspace package. - Requires
bun.lockto matchpackage.json. If you edited dependencies since the last install, runbun installfirst. - Uses the same linker as
bun installwould. Ifnode_moduleswas created with the other linker,bun prunerefuses to run — pass the matching--linker, or runbun install. - Packages are matched by name. A package at the wrong version is left for
bun installto replace. A nested copy (node_modules/a/node_modules/b) is only removed once the correct version is installed above it; otherwise Bun keeps it and prints a warning. - Never removes workspace folders,
.binentries still in use, dot-directories like.cache, plain files, or anything outsidenode_modules. - Packages disabled for the current
os/cpuare removed. Pass--os/--cputo prune for another platform. - Works on a pruned monorepo checkout (e.g.
turbo pruneoutput) the same waybun install --frozen-lockfiledoes. - If any entry fails to delete, the rest are still removed and the command exits
1. --globalis not supported.- To clean the global cache instead, use
bun pm cache rm.