Audit disk usage and remove idle git worktrees, Next.js caches, test Postgres DBs, and unused Bun/npx/uv/Docker caches while preserving active checkouts. Use for /cleanup, /cleanup disk, or freeing worktree/build disk space.
1,163 stars
0 votes
0 copies
0 views
Added November 10, 2026
ai-agentsgoshellnextjsnodedockergitdatabase
Works with
claude code
Security analysis
B77/100
highPerforms destructive filesystem operations
mediumInstalls packages at runtime which could introduce malicious dependencies
Installs into .claude/skills of the current project.
Sponsors
SSkills DirectorySkills Directory
Know which skills are safe — weekly.
SSkills DirectorySkills Directory
Is that skill safe to install?
Are you the author of Cleanup?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/udecode-cleanup)
---
name: cleanup
description: "Audit disk usage and remove idle git worktrees, Next.js caches, test Postgres DBs, and unused Bun/npx/uv/Docker caches while preserving active checkouts. Use for /cleanup, /cleanup disk, or freeing worktree/build disk space."
argument-hint: '[scan|all|disk]'
---
# Cleanup
pstack's Worktree and simulator cleanup playbook is the method, and this skill wins where the two differ. When you remove a worktree yourself, use only `git worktree remove --force`, never `rm -rf` or `git worktree prune`. This skill adds what it lacks for this setup: the runtime's session list, session transcripts and running processes decide which worktrees are in use, uncommitted changes never keep a worktree, Next.js cache folders join the list, and the block's Autopilot rule lets the safe set go without asking. A card session lands its uncommitted changes in the checkout as a patch, so a leftover worktree's changes are usually there already, even when the checkout has edited them since. `scripts/scan.mjs` beside this file makes every verdict, so a rerun reaches the same one. Its `--landed <worktree>` mode checks that the checkout holds every change and ignored file of one worktree. In a pstack project, whose `.agents/pstack.json` names its plans directory, it counts only ignored files under `<plans>/artifacts/`, where a file counts once the checkout has its path, and skips commits only HEAD's reflog reaches, because there every other ignored file is build output or a copy and a landed squash leaves its originals in the reflog. Each file must match byte for byte, except a tracked file another landed patch also edited, whose hunks the checkout's copy must hold. A card session runs it right after its patch lands and then leaves its worktree for the scan. Its `--remove <worktree>...` mode judges nothing. The chips sweep passes it the worktrees of the landed card sessions it archived, and the mode stops each process working in one, saves what it alone holds as `--delete` does, and removes it. It keeps a worktree git does not list, a locked one, one outside the folders agents make worktrees in, one holding another worktree, the one it runs in, one whose commits git cannot list, one with more than 20,000 ignored files to compare, one with an unresolved merge, one whose processes `lsof` cannot list, one whose save fails, one a process starts in or something writes during the save and one git refuses to remove. `--landed` holds back a worktree with such a commit, which only its HEAD history keeps once a switch or reset leaves it, because the removal deletes that history. It holds it back only while the commit writes a file whose content neither the checkout's HEAD nor its upstream has and neither changed since, so the commits a squash landing leaves behind do not. The scan holds back no worktree for its commits, because `--delete` saves them in a bundle before it removes the worktree, as `--remove` does.
`/cleanup disk` finds where the space goes and removes nothing. Run `node <this skill>/scripts/disk.mjs` in the background, because it measures the whole home folder with `du` and took 28 minutes on a busy Mac with 1.8 TB in use. It prints the free space and local snapshots of the volume that holds the home folder, then each folder of at least 10 GB under the home folder, `/private/tmp`, the temp folder and `/Applications`, up to five levels down. It sums the worktrees smaller than that per repository, so a hundred small worktrees show as one row. Each row says `written` when something in it changed in the last day and `quiet` otherwise, and a row of summed worktrees counts the quiet ones. `--min <GB>`, `--hours <n>`, `--root <dir>` and `--json` change what it measures and prints. Report the biggest rows with their state, then run `/cleanup scan` on each repository whose worktrees are among the biggest rows. A quiet row is not safe to delete. Docker's disk image, an app's data and session transcripts go only when the owner names them, because only this skill's rules decide what can go without asking.
`node <this skill>/scripts/machine.mjs` judges the caches package tools keep outside any checkout and the Docker volumes no container uses, and `--delete` clears its `SAFE` rows. `SAFE` covers the Bun runtime transpiler files nothing wrote for a week, because Bun never evicts one, each npx install nothing wrote for a week and no running process uses, `uv cache prune`, and each anonymous Docker volume no container uses. `ASK` covers the pnpm store, which every checkout installs from, and each compose project's named volumes no container uses, which may hold its data. Remove those only when the owner names them, with `docker volume rm` and the names the row lists. `/cleanup` and `/cleanup all` run it after the checkout scan, and so does the daily run.
`/cleanup scan` lists and removes nothing. `/cleanup` removes the safe set. `/cleanup all` removes it in every checkout beside this one, such as each repository in `~/git`. Its steps 3 and 4 pass `--repos <that folder>` instead of `--repo .`, and the scan judges each checkout there that has linked worktrees against one session list. To run it each day without a Claude session, run `node <this skill>/scripts/daily.mjs install` once. It writes and loads a launchd job that runs `daily.mjs run` at 05:30 over `~/git`, or the folder `--folder` names. The run stops orphaned Next telemetry flushers, builds the session list from the desktop app's session files, removes the safe set in every checkout there, clears the `SAFE` rows of `machine.mjs`, writes its report to `~/Library/Logs/cleanup-daily/` and posts a notification with the space freed and the count of `ASK` rows. Those files hold no pin, so a worktree that a pinned session leaves idle for a day goes, after its changes are saved as a patch; lock it with `git worktree lock` to keep it. One busy repository gained about ten subagent worktrees a day. `du` puts each at 2 GB or more, but on APFS a pnpm install clones the package store's files, so the space a worktree alone uses is mostly its builds and dev caches, 0.2 to 8 GB.
A project's session-start hook can run `scan.mjs --brief --auto`. Besides the hint line, it starts `--delete` in the background at most once a day per clone, from the session's own worktree, which that run keeps. It passes no session list, so only a running process, a lock, a server or the day's writes keep a worktree, and it removes only the `SAFE` set, while the `ASK` rows wait for `/cleanup`. Each start rewrites `cleanup/last-run` in the repository's git folder, which then holds the run's report. A user turns the run off with the line `auto cleanup: off` in `~/.claude/pstack-models.md`, and `git worktree lock --reason "<why>" <path>` keeps one worktree.
1. Record `df -h <checkout>`. On macOS, `df -h /` reports the read-only system volume, not the data volume that holds the checkout.
2. List every session, archived ones included, with the runtime's session tool, such as Claude Code desktop's `list_sessions` with `include_archived` and a `limit` above the session count. Save its JSON array to the run directory. Without a session list, a worktree that an idle session left open goes after a quiet day, as in the session-start run below.
3. From the project's checkout, run `node <this skill>/scripts/scan.mjs --repo . --sessions <that file>` in the background, because it measures every folder with `du`. It prints one row per worktree and cache folder, `SAFE`, `ASK` or `KEEP` with its size and reason, and the count and size of each.
- `KEEP` for a worktree: git lists it as locked, the scan runs in it, a running process such as a session, shell or dev server works in it, a running session, or one the owner pinned, claims it, or a server holds a cache inside it. A worktree also stays for a day after its last change, after the last write to a file in it that differs from its commit or that git does not track, other than one the save skips as the `SAFE` row says, or after the last write to a transcript of a session working in it, or of the session whose scratch folder holds it, unless every session that claims it is archived. Claude Code keeps the first kind of transcript in a folder of its `projects` folder named for the worktree path, with each character other than a letter or digit written as a dash. The change clock reads the time of the newest entry in the worktree's HEAD reflog, not the reflog file's modification time, because `git gc` rewrites the reflog of every worktree at once. A Codex chat keeps the worktree it works in for a day after the last write to its rollout, the file under `$CODEX_HOME/sessions/` (`~/.codex` by default) whose first line names the chat's working directory. A compressed `.jsonl.zst` rollout counts too. A rollout written in the last day that the scan cannot read keeps every worktree, because its chat could work in any of them. The scan also keeps it when `lsof` cannot answer.
- `SAFE` for a worktree: it sits where agents make worktrees and nothing above keeps it. No session that claims it is running or pinned, and either every session that claims it is archived or it has been quiet for a day. A session left open but idle keeps it for that day only. Its uncommitted changes, its commits that no branch, tag or remote reaches and its ignored files do not keep it. Before `--delete` removes the worktree, it saves what only the worktree holds to a `<worktree>-<id>` folder under the repository's git folder, in `cleanup/`:
- `changes.patch` holds the uncommitted changes the checkout lacks.
- `staged.patch` holds a staged version that differs from its file.
- `commits.bundle` holds the unreached commits, merges included, and every commit under them that no tag and not `origin`'s default branch reaches, or no remote branch when `origin` names no default, so deleting a local or pull-request branch later loses none of them. A worktree whose folder is already gone gets its bundle too, read from git's record of it.
- `ignored/` holds a copy of each ignored file the checkout lacks or holds with other content, under its own path, because a patch leaves ignored files out. Like `--landed`, it skips ignored files an install or build recreates, such as `node_modules`, and in a pstack project those outside `<plans>/artifacts/`.
- `worktree` names the worktree's path, its branch and its HEAD commit.
It then judges the worktree again and keeps it, deleting the save, when its verdict changed or something wrote it during the save. Its branch stays. A save's folder name ends in `.partial` until the save is whole, and `--delete` deletes a `.partial` folder and a temporary `refs/cleanup/` ref that a run that died left more than a day ago. It deletes each save in `cleanup/`, including a patch an older scan or a run folder's clone left there, a week after the save was last written. It keeps the `last-run` report. A worktree whose every change the checkout already holds gets no patch, because its card's landing patch is the record. A worktree whose folder is already gone is also `SAFE`, because only git's record of it goes, and so is one whose folder lost its `.git` file and holds no files. Agents make worktrees in the app's `.claude/worktrees` and a temp or scratch folder, and older runs left them in the run directory. Agents also add them anywhere, such as beside the checkout: Codex on a branch that starts with `codex/` or on a detached HEAD for a proof, and Claude on a branch that starts with `claude/`. So a worktree on such a branch or a detached HEAD counts as made by an agent wherever it sits, and a person who keeps one by hand locks it. To bring a worktree back from its save within that week, run these from the checkout:
1. When the save holds `commits.bundle`, run `git fetch <save>/commits.bundle 'refs/cleanup/*:refs/restored/*'`. A later `--delete` deletes a day-old ref under `refs/cleanup/`, so the restored refs go elsewhere.
2. Run `git worktree add --detach <path> <HEAD>` with the HEAD commit that `worktree` names, then switch the new worktree to its branch if you want it there.
3. In the new worktree, run `git apply <save>/changes.patch` and `git apply --cached <save>/staged.patch` for each of those files the save holds.
4. Copy the contents of `<save>/ignored/` into the new worktree.
- `ASK` for a worktree: it sits outside those folders, another worktree lives inside it, it holds a submodule or nested repository with changes, its index holds an unresolved merge, whose stages a patch cannot save, git cannot list the commits its HEAD history keeps, it has more than 20,000 ignored files to compare, or its folder lost its `.git` file and still holds files, which git can no longer read.
- `KEEP` for a cache: a running server holds its `dev/lock` or `lock`. Outside a run folder, a build output (`.next` or `.next-plite`) never goes, because `next start` may serve it. Its `dev` and `cache` folders, which Next recreates, get rows of their own instead: `KEEP` while a server holds the build output's lock or something wrote within two levels of the folder in the last day, and `SAFE` after that. Any other `.next-*` folder is judged whole: `KEEP` for a week after its last use when it holds a production build (`BUILD_ID`), for an hour otherwise, and `SAFE` after that. In the main checkout, a port's dev folder (`.next-<port>`) and a build output's `dev` folder stay `KEEP` for a week after their last use, because a project may keep Turbopack's dev disk cache only there, where `next dev` reads it back after a restart. In a worktree a port's dev folder goes after the hour like any other `.next-*` folder, because its Turbopack cache can hold gigabytes that a restart rarely reads back. A cache inside a safe worktree goes with that worktree.
- A run folder is each folder in `<plans>/artifacts/` of a pstack project, whose `.agents/pstack.json` names its plans directory. Each folder in an ignored `.tmp/` at a checkout's root, where runs left scratch copies, is one too, in any project. It gets a row only when it holds an install or build (`node_modules`, `.next`, `.next-*` or `.turbo`) or nothing wrote it for a week. Its quiet time reads only file writes outside those folders. `KEEP` when a worktree lives inside it, a running process works in it or something wrote it in the last day. `SAFE` with `expire`, which deletes the whole folder, when nothing wrote it for a week and neither the ledger nor a decision log in `<plans>/` changed that week names it, because a new row may still cite its files as proof. A clone inside it blocks `expire` when it holds commits no remote has, such as a stash or any commit of a clone with no remote, or when git cannot read it, such as a worktree copy its repository dropped. Such a folder is `SAFE` with `strip` while it holds installs or builds, and `ASK` after that. Before `--delete` expires a folder, it saves each clone's uncommitted changes as a patch under the repository's git folder, in `cleanup/`, as it does for a worktree's. The patch holds each file git does not ignore, every one of them in a repository with no commit yet, and the ignored ones, such as installs, builds and env copies, go with the folder. Otherwise `SAFE` with `strip`, which deletes only its installs and builds and keeps its logs. The scan never touches a file at the top of `<plans>/artifacts/` or `.tmp/`, such as the ledger or a rendered plan page.
- A database row is a database on a local Postgres server that runs in Docker and that an env file (`.env`, `.env.local`, `.env.development`, `.env.development.local`, `.env.test` or `.env.test.local`) of the checkout, a worktree or another checkout beside it points at, through a `localhost`, `127.0.0.1` or `[::1]` URL. `KEEP` while such an env file names it, so the main checkout's database stays, while a connection is open, or for a day after its last write. `SAFE` after that, because test and proof runs make the rest and never drop them. `--delete` judges it once more and drops it without `FORCE`, so a connection opened since the scan keeps it. `ASK` for the whole server when the scan cannot list its databases, such as when its user is no superuser.
4. For `/cleanup scan`, report the table and stop. Otherwise save the session list again as step 2 does, then run the same command with `--delete` and the new file. It measures each safe item, judges it again, lists the running processes once more, saves what a worktree alone holds and judges it again, as the `SAFE` row says, right before removing it, so a folder stays when a process started working in it, or a server took it, during the scan. A desktop session reopened with no process in the folder yet is the one case it cannot see. It removes a worktree with `git worktree remove --force`, and a failed removal turns that row into `ASK`. Never run `git worktree prune` or `git stash`.
5. List each `ASK` row for the owner with its path, size, reason and session title. Remove only the ones the owner names, with `git worktree remove --force <path>`. Lock each one the owner keeps with `git worktree lock --reason "<why>" <path>`, so later scans and the session-start hint skip it.
6. Record `df -h <checkout>` again and report the space freed from the two `df` readings, because `du` counts the hard links and APFS clones a package store shares once per folder. Then report what went and each row kept or asked, with its reason.
Run it once in each project checkout whose worktrees or caches the owner wants gone. A session leaves its own worktree once its task lands, as the chips Landing rule says, so the scan finds those after their quiet day, along with proof worktrees a run left behind, dev caches and subagent worktrees. Claude Code names a subagent's worktree `agent-<id>`, locks it while the subagent runs, and keeps it once the subagent has changed a file.
A `next dev` run spawns a detached process that flushes Next's telemetry and should exit within seconds. When it starts an esbuild service, as a Next config that loads `@workflow` builders does, it never exits, and the scan keeps the worktree it works in. If a `KEEP` row's processes have parent PID 1 and run `next/dist/telemetry/detached-flush.js`, stop them, run `next telemetry disable` once so no new ones start, and scan again.