A lerd plugin that removes finished git worktrees, with their dev server and their own database, by hand or on a schedule, following rules you choose.
Every worktree of a proxy site runs its own dev server (a file watcher) and a
Laravel worktree can carry its own database, so finished worktrees pile up and
keep fseventsd busy. "Finished" means: its PR was merged, its PR was closed,
or (detached / no PR) its HEAD is already on a base branch and has been for a
day. Worktrees a process is sitting in, or that lerd saw recent traffic on, are
kept. Uncommitted changes are saved as a patch before removal, or the worktree
is kept, per your settings.
lerd plugin install https://github.com/bikramtuladhar/lerd-plugin-worktree-cleanup
Usage, every setting and the required tools: DOCS.md. What changed in each version: CHANGELOG.md.
- A dashboard page, Worktree cleanup: Preview (dry run) and Clean up now shown live as they run, a browsable history of past runs, settings with a plain-language summary of your rules, and a Tools tab with a system check, storage use and saved patches.
- Commands
worktree-cleanup:run,:config. - A schedule,
cleanup, that lerd runs every 15 minutes once you allow it; the plugin acts only onceSCHEDULE_EVERYhas passed. - A site command, Clean up finished worktrees, for one site.
- An MCP tool,
worktree-cleanup_run, plus a skill telling assistants to dry run first. - A loopback-only API endpoint the page uses.
- lerd's
/api/sites(sites, their worktrees, branch, idle state and whether a worktree has its own database). - Each site's git repository:
git fetch originof the base branches, worktree status, whether HEAD is on a base branch. - Your global git
core.sshCommandsetting and theGIT_SSH_COMMANDvariable, only to check whether you set your own SSH command (see What it sends). - The working folder of running processes (
ps,lsof), to skip worktrees in use. lerd plugin schedule worktree-cleanup, to show on the Settings tab whether the schedule is allowed and when it last ran.- The folders it searches for
gh: whateverPATHlerd passes, then~/.local/bin,~/bin,/opt/homebrew/bin,/usr/local/bin,/home/linuxbrew/.linuxbrew/bin,~/.linuxbrew/binand/snap/bin.
- Removes finished worktrees with
lerd worktree remove(and--forceonly when your rules say to remove one with uncommitted changes). - Drops a worktree's own database with
lerd db:sharebefore removing it. git fetch originupdates the site'soriginremote-tracking refs of the base branches (refs/remotes/origin/mainand so on). Local branches and working trees are not touched.- Before saving a patch of a worktree with uncommitted changes, runs
git add --intent-to-add --allin that worktree so untracked files are in the patch. Only the worktree about to be removed. - If
lerd worktree removefails after git has already unregistered the worktree (a dev server was still writing to it), deletes the worktree's folder itself and runsgit worktree prunein the site. Only a folder below the site's own folder or next to it, never the site itself; its changes were committed or saved as a patch first. - Writes only to its data folder:
config.env(andconfig.env.tmpwhile saving it),cleanup.log,runs/(the last 200 runs, as JSON lines),patches/,last-scheduled-run,run.lock,last-run.err(errors from starting a dashboard run), and a temporarytmp.XXXXXXfolder per run, deleted when the run ends (lerd writesschedule.logthere too). - Schedule (off until you allow it): lerd asks at install, or turn it on
with the Schedule switch on the plugin's card or
lerd plugin schedule worktree-cleanup on. lerd has no "PR merged" event, so this is how "clean up after a merge" happens without you running it.
- GitHub, through the
ghCLI you are logged into: one GraphQL query per site'soriginrepository, asking for the latest PR of each worktree's branch and the PRs containing each worktree's HEAD commit (state, number). - GitHub again, only when you open the Tools tab or run
worktree-cleanup:config check: your login (gh api user) and how many GraphQL points you have left (gh api rate_limit), to show in the system check. - Each site's
originremote, throughgit fetchof the base branches. That can be any git host, not only GitHub, over whatever protocol the remote uses. Over SSH it runs withBatchMode=yes,ConnectTimeout=15,ServerAliveInterval=10andServerAliveCountMax=3, so it never prompts and gives up on a stalled connection, unless you set your owncore.sshCommandorGIT_SSH_COMMAND, which are then used as they are. Nothing else leaves the machine. - lerd's own API:
/api/sitesand, only after a scheduled run removes something, a dashboard notification (NOTIFY).
macOS or Linux, lerd 1.39+ (for schedules), gh (logged in), git, jq,
curl, lsof. The dashboard starts runs in the background with setsid
(Linux) or perl (macOS ships it).
MIT
