---
description: "Sweep leftover /tmp scratch (picker state, review diffs, channel payloads, analysis drafts), worktree residue (orphan .worktrees dirs, gitlink index entries) and offloaded tool payloads under .multi-agent/refs/ from past runs. With --abandoned, also reaps runs that stopped and were never picked back up, stashing any uncommitted work. Dry-run first; confirms before deleting. Use when scratch files from earlier runs need clearing out, or when stopped runs are holding worktrees."
disable-model-invocation: true
description-tr: "Geçmiş çalışmalardan kalan /tmp scratch'ini (picker durumu, review diff'leri, kanal payload'ları, analiz taslakları), worktree kalıntılarını (sahipsiz .worktrees klasörleri, gitlink index kayıtları) ve .multi-agent/refs/ altındaki offload edilmiş araç çıktılarını süpürür. --abandoned ile durup bir daha dönülmemiş koşuları da toplar, commit edilmemiş işi stash'ler. Önce dry-run; silmeden önce onay alır."
argument-hint: "[--older-than=<minutes>] [--abandoned [--days=N]] [--yes]  -  dry-run unless confirmed"
parameters: [{"name":"--older-than","kind":"text"},{"name":"--abandoned","kind":"flag"},{"name":"--days","kind":"text"},{"name":"--yes","kind":"flag"}]
gui: button
destructive: true
confirm: required
---

# multi-agent garbage-collect  -  Sweep /tmp Scratch

> **Pickers follow** `$HOME/.claude/multi-agent-refs/picker-contract.md`: never a one-option call, and branch on the option selected, not on its text.

Each run writes ephemeral scratch under `/tmp`: picker state
(`multi-agent-picker-*`), PR/review diffs (`multi-agent-review-*`), issue
progress bodies (`issue-progress-*`), channel payloads (`channels-*`),
external-context bundles (`context-links-*` / `context-by-type-*`), and analysis
drafts (`analysis-*`). Most are never cleaned up, so a long-lived machine
accumulates them. This sweeps the known prefixes only.

Backed by `$HOME/.claude/scripts/gc-tmp.sh`. Safe by default: it previews and
deletes nothing until you confirm.

## Steps

1. **Preview (dry-run)**  -  run the sweeper with no `--yes`, forwarding an
   `--older-than=<minutes>` argument if the user supplied one:

   ```bash
   bash $HOME/.claude/scripts/gc-tmp.sh ${ARGUMENTS}
   ```

   It prints each matching item and the space it would free.

2. **Nothing to do**  -  if the output says `nothing to do`, stop and report
   that there is no scratch to clean.

3. **Confirm**  -  surface a native `AskUserQuestion` picker (no typed keyword),
   in `outputLanguage`:
   - `question`: "Delete the listed /tmp scratch items now?" (include the count
     and freed-size estimate from step 1)
   - `header`: "Sweep" (English, <=12 chars)
   - `options`: `{ label: "Delete scratch", description: "Remove the listed /tmp items" }`,
     `{ label: "Cancel", description: "Keep everything, change nothing" }`
   - Anything other than **Delete scratch** -> cancel and change nothing.

4. **Delete**  -  on confirm, re-run with `--yes` (and the same
   `--older-than` if given):

   ```bash
   bash $HOME/.claude/scripts/gc-tmp.sh --yes ${ARGUMENTS}
   ```

5. **Report**  -  echo the `══ garbage-collect: removed N item(s), freed ~X ══`
   summary line.

6. **Worktree residue sweep (second phase)**  -  when the current directory is
   a git repo, also preview worktree leftovers:

   ```bash
   bash $HOME/.claude/scripts/gc-worktrees.sh ${ARGUMENTS}
   ```

   This covers: stale worktree admin entries (`git worktree prune`, applied
   immediately  -  it only drops bookkeeping for dirs that are already gone),
   orphan `.worktrees/*` dirs no longer registered as worktrees, gitlink
   residue in the index (`.worktrees/{id}` recorded as a "Subproject commit"
   entry by a pre-guard `git add -A`), and a missing `.worktrees/` line in
   `.git/info/exclude`. Registered, healthy worktrees are NEVER touched  -
   those belong to `/multi-agent:resume` / `/multi-agent:kill`.

   - Output says `nothing to do` -> skip silently, no question.
   - Otherwise surface a second `AskUserQuestion` (in `outputLanguage`):
     `question`: "Apply the listed worktree cleanups?" (include the dry-run
     lines), `header`: "Worktrees", options `{ label: "Apply cleanups" }` /
     `{ label: "Cancel", description: "Keep everything, change nothing" }`.
   - On confirm, re-run with `--yes` and echo its summary line. If a gitlink
     was unstaged, remind the user the removal still needs a commit.

7. **Offloaded payload sweep (third phase)**  -  when the current directory is a
   git repo, also preview the offloaded tool payloads:

   ```bash
   bash $HOME/.claude/scripts/gc-refs.sh ${ARGUMENTS}
   ```

   `offload-ref.sh` parks build logs, diffs and test output under
   `.multi-agent/refs/<node_id>.md`. In worktree modes that directory dies with
   the worktree; when the run chose a local workspace it lands in the real checkout, and
   because it is gitignored it never shows up in `git status`. Only files
   matching the node-id shape are touched  -  anything else in that directory is
   left alone, and files inside the grace window are spared so a sweep cannot
   pull a ref out from under a running phase.

   - Output says `nothing to do` -> skip silently, no question.
   - Otherwise surface a third `AskUserQuestion` (in `outputLanguage`):
     `question`: "Delete the listed offloaded payloads?" (include the count and
     freed size), `header`: "Payloads", options `{ label: "Delete payloads" }` /
     `{ label: "Cancel", description: "Keep everything, change nothing" }`.
   - On confirm, re-run with `--yes` and echo its summary line.

   Pass `--all` to sweep every checkout under `$HOME` rather than only the
   current repo.

8. **Abandoned runs (fourth phase, only with `--abandoned`)**  -  the other three
   phases sweep leftovers from runs that ENDED. This one reaps runs that simply
   stopped:

   ```bash
   bash $HOME/.claude/scripts/gc-abandoned.sh ${ARGUMENTS}
   ```

   Nothing else collects these. `gc-worktrees.sh` skips registered worktrees on
   purpose, because a registered worktree belongs to a live run - and a run that
   stopped never stops being "live". Measured on one machine: 20 runs reading
   `in_progress`, every one over a day old, one of them 157 days.

   The output has four kinds of line and they are not interchangeable:

   | Line | Meaning |
   |---|---|
   | `would remove` / `would remove worktree` | reapable: stopped past its age limit, or finished with its worktree left behind |
   | `would stash` | uncommitted work found - it goes into a stash entry labelled `autopilot/abandoned/<task-id>` (`git stash list`) and the worktree is **kept** |
   | `would mark abandoned` | the state is stale but there is no worktree left to remove |
   | `unattributed` | a worktree with no run state. **Never removed, whatever the flags.** `.worktrees/` is not exclusively ours, and nothing distinguishes a hand-made worktree from a pipeline one whose log was pruned. Report the list and its size; the user decides |

   Three rules hold with or without `--yes`, and they are why this is safe to
   offer: a run waiting on you (open PR, Phase 4/5, `awaiting_input`) is never
   touched; a path outside `<repo>/.worktrees/` is never removed, which matters
   because four state files on the measured machine record the REPO ROOT as
   their `worktreePath`; and uncommitted work is stashed rather than lost.

   - Output says `nothing to do` -> skip silently, no question.
   - Otherwise surface a fourth `AskUserQuestion` (in `outputLanguage`):
     `question`: "Reap the listed abandoned runs?" (include the counts, the
     freed-size estimate, and the unattributed total as a separate sentence -
     those are not being reaped), `header`: "Abandoned",
     options `{ label: "Reap runs" }` / `{ label: "Cancel", description: "Keep everything, change nothing" }`.
   - On confirm, re-run with `--yes` and echo its summary line. If anything was
     stashed, name the branch so the user can recover it.

## Notes

- An in-flight analysis keeps its drafts in `/tmp/analysis-<slug>-<timestamp>/`
  and `/multi-agent:resume` re-uses them. If a run is paused at
  `awaiting_output_decision`, pass `--older-than=<minutes>` so fresh drafts are
  spared, or finish the run first.
- The /tmp phase only touches `/tmp` scratch; the worktree phase only touches
  the current repo's `.worktrees/` residue and index; the payload phase only
  touches node-id files under `.multi-agent/refs/`. Persistent per-task logs
  live under `~/.claude/logs/multi-agent/` and are cleaned by
  `/multi-agent:prune-logs`. The per-repo memory stores under
  `~/.claude/memory/multi-agent/` are durable knowledge, not scratch: they are
  removed only by `/multi-agent:uninstall --all-data`, or one entry at a time
  with `learnings-ledger.mjs forget`.
