# Cleanup

> Remove what this run created once it has merged or the run ends, and only that, with proof for each removal. Use when asked to clean up, tidy up after a run, re

- Skill: `ilexpwh/cleanup` (Agent Skill)
- Install (CLI): `npx get-skill add ilexpwh/cleanup`
- Raw SKILL.md: https://api.skillmd.com/api/skills/ilexpwh/cleanup/raw
- Safety review: pending
- Works with: any agent that reads SKILL.md (Claude Code, Claude.ai, Cursor, Codex, Windsurf, 60+ more)
- Category: Coding & Dev Tools
- Author: Ilexpwh (https://skillmd.com/u/ilexpwh)
- Updated: 2026-12-02
- Page: https://skillmd.com/skills/ilexpwh/cleanup

---

---
name: cleanup
description: Remove what this run created once it has merged or the run ends, and only that, with proof for each removal. Use when asked to clean up, tidy up after a run, remove stale or merged worktrees, delete merged branches, prune worktrees, free disk space taken by old checkouts, kill leftover processes or port-forwards, restore temporary config or delete temp files. Not for tidying code or commits (land) or pausing a run that isn't finished (the pause-safely playbook).
metadata:
  phase: ship
---

# Cleanup

Leave the host as you found it: remove the worktrees, branches, processes, temporary config and files this run created and can prove are finished, and keep everything else.

## When to use
- After a PR merges, or at the end of a run (the last step of most playbooks).
- Worktrees or branches have piled up and the user asks to clear them.
- A run started processes, port-forwards or config changes that must not outlive it.

**Not for:** cleaning up code or commit history (`../land/SKILL.md`), or parking unfinished work (`../istack-mode/playbooks/pause-safely.md`).

## Steps
`ledger <cmd>` below means `python3 -I "$LEDGER" --root <workspace> <cmd> --task <task>`, with `LEDGER` at `../ledger/scripts/ledger.py` (Claude Code: `${CLAUDE_SKILL_DIR}/../ledger/scripts/ledger.py`). `run/` is the task's run folder. Commands are in `references/proofs.md`.

1. **List what this run created.** Build the candidate list from the ledger, not from the disk: tracker PRs give head branches; worker briefs (`run/briefs/`) and the checkpoint give worktree paths, process ids, ports, temporary config and temp paths. Read worktree paths from `git worktree list --porcelain`, never type them. Anything you can't trace to this run goes straight to **kept**. *Done when:* every candidate has a row with its origin in the ledger.
2. **Prove each one is finished.** A branch is finished only when its PR reads `MERGED` and the local tip equals the PR's head commit, or `git branch --merged origin/main` lists it. A worktree must also be clean: no modified or untracked files, no commits beyond the merged head, no running worker or shell inside it. Anything open, dirty, ahead or in use is kept. *Done when:* every row says `remove` with its proof, or `keep` with its reason.
3. **Stop processes you started.** Stop each by its recorded process id or the host's task id, then confirm it is gone and its port is free. Never kill by name or pattern; other agents run the same tools. *Done when:* no recorded process is alive and each recorded port is free.
4. **Restore temporary config.** Put back every value the run changed for testing: kube context or namespace, git config, environment files, feature flags or replicas on staging. Use the original value recorded in the checkpoint. If none was recorded, leave it and report it. *Done when:* each recorded setting shows its original value.
5. **Remove worktrees, then branches.** `git worktree remove <path>` without `--force`; if git refuses, keep it and report why. Run `git worktree prune` afterwards. Delete branches with `git branch -d`; use `-D` only for a squash-merged branch whose step 2 proof holds. Delete a remote branch only if the host didn't already, the tier allows pushes, and nobody else pushes to it. *Done when:* `git worktree list` and `git branch` no longer show the removed rows.
6. **Delete temp files.** Remove only paths you created: scratch folders, payload files, downloaded logs. Never touch `run/`; the ledger, evidence and messages are the record. *Done when:* each recorded temp path is gone and `run/` is intact.
7. **Record the result.** Write `run/evidence/cleanup-<date>.md` with the removed and kept tables, then checkpoint the ledger. *Done when:* the file exists and the checkpoint points to it.

## Red flags
| If you're thinking | Do this instead |
|---|---|
| "These 100 worktrees look stale, clear them all." | Remove only what you can trace to this run and prove finished. List the rest as kept. |
| "`--merged` doesn't list it, so `-D` it anyway." | Prove it: PR `MERGED` and the tip equals the PR head. Otherwise keep it. |
| "`git worktree remove --force` is quicker." | Force discards uncommitted work. Keep it and report what's inside. |
| "`pkill -f port-forward` will catch them all." | Stop by recorded process id only (`clean-shared-hosts`). |
| "Stash the leftovers so the worktree is clean." | Never a bare `git stash`; worktrees share one stash stack. Keep and report. |
| "Approved is as good as merged." | Only `MERGED` counts. Open PRs keep their branch and worktree. |
| "I'll tidy the evidence folder too." | `run/` is the ledger. Leave it. |

## Verify
- [ ] Every removed item traces to this run in the ledger and has a merge or ownership proof.
- [ ] No `--force` worktree removal, no `-D` without proof, no pattern kill, no bare stash.
- [ ] Recorded processes are gone and their ports are free.
- [ ] Temporary config shows its original values, or is listed as not restored.
- [ ] `run/` is intact and `run/evidence/cleanup-<date>.md` exists.

## Reply
```
Cleanup <task>: removed <n>, kept <n>
Removed:
| Kind | Path or name | Proof |
|---|---|---|
| worktree | <path> | PR <n> MERGED, clean |
Kept:
| Kind | Path or name | Why |
|---|---|---|
| branch | <name> | PR <n> still open |
Stopped: <pid or task id → port>. Restored: <setting → original value>. Not restored: <setting, why>
Record: run/evidence/cleanup-<date>.md
```

## Credits
"The bucket is advice, not permission", reading paths from `git worktree list` and pausing on uncommitted work follow pstack's `worktree-cleanup` playbook by Lauren Tan. The rest applies this pack's `clean-shared-hosts` principle.

