Skip to main content

Let your agents cleanup after they leave your desk

If you are let multiple agents work in parallel on your code base, you need a way to start an agent session with code isolation. The typical way to do this is by creating separate Git worktrees for each agent. avoids that parallel agents overwrite each other’s files. But the sessions pile up, and so do the worktrees. Each one is a full checkout on your disk.

Until recently I did the cleanup work myself, but now VSCode has a worktree cleanup feature to deal with this.

Why do we need this?

Before this feature, you had two options: do it by hand or ignore it.

By hand meant something like this:

git worktree list
git worktree remove <path>
git worktree prune

That works. But it doesn’t tell you which worktree belongs to which agent session, whether the session is still relevant, or how much space you’ll get back.

Archive, done or delete?

Cleanup builds on how VS Code already finishes sessions. There are three actions:

  • Close a chat tab: hides the conversation, nothing is deleted.
  • Archive (Chat view) or Mark as Done (Agents window): removes the session from the active list but keeps the conversation.
  • Delete: removes the conversation permanently.

When you archive a worktree session, VS Code commits uncommitted changes to the session branch and then removes the worktree folder. The branch and commits stay, so restoring the session re-creates the worktree.

That’s fine for one session. It doesn’t scale to thirty.

Using the worktree cleanup editor

Open the command palette in the VSCode Agents Window and run Chat: Open Worktree Cleanup.

 


Then:

  1. Choose an inactivity filter and, if needed, a workspace.
  2. Review the selected worktrees. You see the branch, the path and an estimated size for each. Select the ones you want to delete.
  3. Click on Clean Up Worktree.

So, what happens when you confirm? Normal cleanup does the following:

  • marks the listed sessions as done
  • commits non-ignored uncommitted changes to their branches
  • schedules the worktree folders for removal

Branches and session history are kept. If you need a session later, restore it and the worktree is re-created.

Remark: Ignored files are not committed. Anything in .gitignore (build output, node_modules, local config) is deleted together with the worktree. Normal cleanup also protects worktrees that are used by other sessions or chats.

The cleanup editor also finds worktrees you’d never find through the sessions list:

  • Leftover worktrees from done or archived sessions. Marking a session as done doesn’t guarantee its worktree was removed.
  • Managed worktrees without a listed session. These are Git-registered worktrees under the <repository>.worktrees directory. Worktrees you created yourself elsewhere are excluded.

Remark: The sizes are estimates. An unknown size doesn’t mean zero storage, and a failed cleanup doesn’t confirm the worktree is gone. If something fails, use Retry Failed Worktrees.

Automatic cleanup of merged sessions

It even gets better and you have the option to configure automatic cleanup. 


The first checkbox marks a session as done after the given number of days of inactivity. The second checkbox permanently deletes it after a grace period. 

A session is only eligible when it has a merged pull request and no related open pull requests. Automatic cleanup is also more careful than manual archiving. It only removes a worktree if the branch tracks an upstream and has no outgoing commits or uncommitted changes. It never force-removes.

That’s it! Run it once, look at the size of what you get back, and decide if you want it automated.

More information

Popular posts from this blog

Podman– Command execution failed with exit code 125

After updating WSL on one of the developer machines, Podman failed to work. When we took a look through Podman Desktop, we noticed that Podman had stopped running and returned the following error message: Error: Command execution failed with exit code 125 Here are the steps we tried to fix the issue: We started by running podman info to get some extra details on what could be wrong: >podman info OS: windows/amd64 provider: wsl version: 5.3.1 Cannot connect to Podman. Please verify your connection to the Linux system using `podman system connection list`, or try `podman machine init` and `podman machine start` to manage a new Linux VM Error: unable to connect to Podman socket: failed to connect: dial tcp 127.0.0.1:2655: connectex: No connection could be made because the target machine actively refused it. That makes sense as the podman VM was not running. Let’s check the VM: >podman machine list NAME         ...

Cache stampede: when our cache turned against us

While investigating some performance issues, we ran into an ASP.NET Core API that cached a fairly expensive aggregation query for 60 seconds. Under normal load, that was fine: one request rebuilds the cache, everyone else reads from it. Under peak load, dozens of requests would arrive in that same expiry window, all see a cache miss, and all fire the same expensive query in parallel. The database didn't like that. That was the moment when our caching layer stopped helping and started hurting. A burst of requests comes in at the same time, all miss the cache, and all go hammer the database or the downstream API at once. That's a cache stampede . The cache was supposed to protect our backend, and for a few hundred milliseconds it did the opposite. Why this happens IMemoryCache.GetOrCreate (and its async sibling) looks like it protects you, but it doesn't add any locking on its own. Look at the naive version: public async Task<Report> GetReportAsync(string key) ...

VS Code Planning mode

After the introduction of Plan mode in Visual Studio , it now also found its way into VS Code. Planning mode, or as I like to call it 'Hannibal mode', extends GitHub Copilot's Agent Mode capabilities to handle larger, multi-step coding tasks with a structured approach. Instead of jumping straight into code generation, Planning mode creates a detailed execution plan. If you want more details, have a look at my previous post . Putting plan mode into action VS Code takes a different approach compared to Visual Studio when using plan mode. Instead of a configuration setting that you can activate but have limited control over, planning is available as a separate chat mode/agent: I like this approach better than how Visual Studio does it as you have explicit control when plan mode is activated. Instead of immediately diving into execution, the plan agent creates a plan and asks some follow up questions: You can further edit the plan by clicking on ‘Open in Editor’: ...