Dev.to AI πŸ€– Ai πŸ‘ 0 πŸ“– 4 min read

Your coding agents filled the disk with worktrees. Here's how to delete them without losing work

Disclosure: I work on Git A, a home for code where many AI agents work on one main branch. Everything below except the last section is plain Git and works without our product. This week a post about coding agents fillin

Disclosure: I work on Git A, a home for code where many AI agents work on one main branch. Everything below except the last section is plain Git and works without our product.

This week a post about coding agents filling a Mac with worktrees passed 100,000 views on X. Another developer wrote that agents "will spawn 200+ worktrees and simulators and will NEVER cleanup after themselves", eating 4 TB overnight. If you run Claude Code, Codex or Cursor agents in parallel, you have probably seen it too.

The hard part isn't freeing the space. It's knowing which folders are safe to delete. One of those posts ended with a 9.3 GB worktree, deleted in a hurry, that held the only copy of something its owner needed.

Why worktrees pile up

Parallel agents need separate working folders, and git worktree add is the cheap, standard way to give them one. But each worktree is a full checkout, and then the agent installs dependencies and builds in it: node_modules, target/, .build, DerivedData, simulator images. Ten agents means ten copies of all of that.

Agents create worktrees readily and remove them rarely. Removing one means deciding the work in it is finished or saved, and an agent mid-task can't be sure. So it leaves the folder, and so does the next one.

What "safe to delete" actually means

A worktree is safe to delete when everything in it exists somewhere else:

  1. No changed or untracked files. git status --porcelain --untracked-files=all prints nothing.
  2. No commits that exist only here. git rev-list --count HEAD --not --remotes prints 0: every commit on this branch is already on some remote.
  3. No ignored files you care about. Ignored files (node_modules, build output) are usually rebuildable, but this is exactly where a data set, a .env or a local database hides. Check before deleting anything large.

Folders you deleted by hand, without git worktree remove, leave stale entries behind. git worktree prune clears them.

A script that only deletes what's safe

This runs a dry run by default. It lists every worktree except your main checkout, with its size, and marks it SAFE or KEEP. With --delete it removes only the SAFE ones. It never uses --force.

#!/usr/bin/env bash
# safe-worktree-cleanup.sh: list agent worktrees, delete only the ones that hold nothing unique.
# Usage: bash safe-worktree-cleanup.sh [--delete]   (run anywhere inside the repo)
# Dry run by default. Needs git 2.17+.
set -uo pipefail
DELETE=0; [[ "${1:-}" == "--delete" ]] && DELETE=1
git fetch -q --all --prune || echo "warning: fetch failed, 'pushed' is judged from old remote refs"
first=1
git worktree list --porcelain | sed -n 's/^worktree //p' | while IFS= read -r wt; do
  if (( first )); then first=0; continue; fi          # the first entry is the main checkout
  if [[ ! -d "$wt" ]]; then echo "GONE  $wt (folder already deleted; prune will drop it)"; continue; fi
  size=$(du -sh "$wt" 2>/dev/null | cut -f1)
  changed=$(git -C "$wt" status --porcelain --untracked-files=all | wc -l | tr -d ' ')
  local_only=$(git -C "$wt" rev-list --count HEAD --not --remotes 2>/dev/null || echo "?")
  ignored=$(git -C "$wt" status --porcelain --ignored=matching | grep -c '^!!')
  if [[ "$changed" == "0" && "$local_only" == "0" ]]; then
    echo "SAFE  $size  $wt  ($ignored ignored paths will be deleted too)"
    if (( DELETE )); then git worktree remove "$wt" && echo "      removed"; fi
  else
    echo "KEEP  $size  $wt  ($changed changed files, $local_only commits on no remote)"
  fi
done
git worktree prune

Sample output:

SAFE  2.1G  ../wt-fix-login   (3 ignored paths will be deleted too)
KEEP  1.8G  ../wt-refactor    (4 changed files, 0 commits on no remote)
KEEP  640M  ../wt-tests       (0 changed files, 2 commits on no remote)

For a KEEP worktree, either push its branch (then it turns SAFE) or decide the work is dead and delete it on purpose. Look at the "ignored paths" count before using --delete: a large number in a folder you don't recognise deserves a quick git status --ignored.

I tested it on a throwaway repo: it removed the two fully pushed worktrees and kept the one with an unsaved file and the one with a local-only commit.

Habits that stop the pile-up

  • One worktree per task, named after the task, so you can tell them apart a week later.
  • Push the branch early, even if the work is unfinished. A pushed branch makes the folder disposable.
  • Put build caches in one shared place (a shared pnpm store, CARGO_TARGET_DIR, a build cache) instead of one per worktree.
  • Run the cleanup on a schedule, not when the disk is already full and you're deleting folders at random.

Where Git A fits

We built Git A for teams running many agents on one repository, and losing work in local folders is one of the problems it takes away. On Git A, an agent's unsaved changes and commits not pushed yet are saved every few seconds. If the laptop stops, the session ends or the AI credits run out, the work saved up to that moment is still there. You can resume it on any machine, or park or discard it with one command. All agents land on one main branch: in our 2 Nov 2026 test, 10 agents putting a change on main at the same moment took 0.69 s with no rejected pushes on Git A, against 27.17 s and 45 rejected pushes on GitHub.com (median of 5 runs, CI not included).

If that sounds useful, git--a.com explains it, and you can measure the rejected pushes on your own repo. If you run a single agent, plain Git and the script above are all you need.

How many worktrees do your agents leave behind in a typical week?

πŸ“° Read the original article on Dev.to AI

Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.