Git worktrees are useful when developers need to work on multiple branches without repeatedly switching the main working directory. They are particularly valuable when using AI coding agents because each agent can work in a separate directory and make changes without interfering with another task.
However, this flexibility creates a storage management problem. Every worktree has its own working directory, dependencies, build outputs, and temporary files. When developers create several agent sessions, these directories can accumulate quickly.
The problem becomes more noticeable when agent sessions run on remote machines. A developer may use VS Code to connect to a remote development host, start multiple agent sessions, and create separate worktrees for each task. Even after the work is complete, the remote directories may continue consuming disk space.
Visual Studio Code provides a Worktree Cleanup experience to manage these resources. Recent VS Code releases have extended this functionality to supported remote Agent Hosts, allowing developers to review and remove inactive worktrees without manually connecting to each machine and deleting directories.
This article explains how Git worktrees work, why remote cleanup matters, how to use the VS Code cleanup interface, what happens to branches and uncommitted changes, and how to troubleshoot common cleanup problems.
Understanding Git Worktrees in Visual Studio Code
A Git worktree is an additional working directory associated with an existing Git repository.
Normally, a Git repository has one primary working directory. When you switch branches, Git updates the files in that directory to match the selected branch.
Worktrees provide another approach. They allow multiple working directories to use the same repository history while checking out different branches.
For example, consider a project with the following structure:
my-application/
main-worktree/
src/
tests/
package.json
feature-login/
src/
tests/
package.json
feature-payment/
src/
tests/
package.json
Each directory represents a separate working tree associated with the repository.
The main worktree might contain the main branch, while feature-login and feature-payment contain separate feature branches.
The worktrees share Git history, branches, and repository objects, but their working files and uncommitted changes remain separate.
Why Developers Create Multiple Worktrees
Worktrees are particularly useful when several development tasks need to run independently.
For example, a team might have three tasks:
Implementing a new authentication flow.
Fixing a payment processing issue.
Updating the application's dashboard.
Each task can use a separate worktree and branch.
The developer can inspect one implementation while another agent continues working on a different task.
This approach reduces the risk of unrelated changes interfering with each other. It also avoids repeatedly switching the primary repository between branches.
However, every additional worktree requires storage. Large projects may contain dependency folders, build outputs, test results, and other generated files in each working directory.
When several sessions are created over time, cleanup becomes important.
Why Remote Worktree Cleanup Is Different
Local worktrees are stored on the developer's computer. They can usually be inspected and removed using local Git commands or VS Code's Source Control features.
Remote worktrees are different because their files exist on another machine.
For example, a developer may connect to a remote Agent Host and start an agent session there. The agent creates a worktree on that host and performs its work inside the remote directory.
The VS Code client displays and manages the session, but the actual worktree files remain on the remote machine.
This creates several practical challenges:
Developers may not know how many worktrees exist on each host.
Inactive agent sessions can leave behind large directories.
Connecting to every remote host manually takes time.
Identical folder paths may exist on different machines.
Deleting a directory without reviewing its Git state can result in lost changes.
VS Code's remote Worktree Cleanup experience addresses these problems by showing eligible worktrees together with their owning hosts.
The cleanup process operates on the selected host rather than deleting a similarly named directory from the developer's local computer.
It is important to distinguish a remote Agent Host from a Git remote. A Git remote such as origin represents a repository hosted on a service or server. A remote Agent Host is the machine that runs the agent session and stores its working files. Cleaning up a remote worktree does not delete the hosted Git repository.
How Worktree Cleanup Works in VS Code
The Worktree Cleanup experience provides a centralized interface for reviewing inactive agent-managed worktrees.
Developers can filter worktrees, inspect their locations, review storage estimates, and select which directories should be removed.
The cleanup process also handles session state and changes in a way that is different from manually deleting a folder.
For eligible selected worktrees, VS Code:
Marks the associated sessions as done.
Commits nonignored changes, including untracked files, to the associated branches.
Schedules the worktree directories for removal.
Retains the branches and session history.
Protects worktrees that are still being used by other sessions or chats.
This behavior is important because cleanup is not simply a command to delete directories.
VS Code attempts to preserve the work that belongs to the selected sessions while reclaiming the storage used by their working directories.
What Happens to the Git Branch?
The branch associated with an agent-managed worktree is retained during the cleanup process.
For example, suppose an agent created a worktree using the feature-login branch.
After cleanup, the worktree directory is scheduled for removal, but the branch remains in the repository.
The developer can later restore the associated session. VS Code recreates the worktree using the retained branch.
This makes it possible to reclaim disk space without permanently removing the development history associated with the session.
However, the retained branch should not be confused with a merged branch. Cleanup does not automatically merge the work into main or push it to a remote repository.
Review and integrate the branch separately when the implementation is ready.
What Happens to Uncommitted Changes?
VS Code's cleanup workflow preserves eligible nonignored changes by committing them to the associated branch before removing the worktree.
This includes untracked files.
For example, suppose a worktree contains:
feature-login/
src/
authentication.ts
tests/
authentication.test.ts
notes.md
If these files contain changes that have not been committed, the cleanup process can preserve them in a commit on the associated branch.
This is different from manually removing a worktree using a force-delete operation.
Developers should still review the changes before cleanup. A commit created to preserve work is not the same as a reviewed and approved feature commit.
What Happens to Ignored Files?
Ignored files require special attention.
Files excluded through .gitignore are not included in the cleanup commit. They are deleted when the worktree directory is removed.
For example, a worktree may contain:
feature-login/
src/
node_modules/
.env
dist/
package.json
Depending on the project's .gitignore configuration, node_modules, .env, and dist may be ignored.
If these files contain local configuration, test data, or other information that must be retained, copy them somewhere safe before confirming cleanup.
Do not assume that all local files will be preserved just because the Git changes are committed.
Cleaning Up Remote Worktrees From Visual Studio Code
Let's walk through the cleanup process.
Step 1: Connect to the Remote Agent Host
Start by connecting to the remote host that contains the worktrees.
Make sure the host is connected and available in VS Code.
Remote cleanup requires a connected host that supports the cleanup operation. If the host is disconnected or does not support cleanup, its worktrees may be unavailable for selection.
Step 2: Open the Worktree Cleanup Editor
You can open the cleanup interface from the Command Palette.
Press Ctrl+Shift+P on Windows or Linux, or Cmd+Shift+P on macOS.
Search for:
Chat: Open Worktree Cleanup
Select the command to open the cleanup editor.
The cleanup editor is available from the regular editor window and the Agents window in supported VS Code versions.
Step 3: Select an Inactivity Filter
The cleanup editor allows you to filter worktrees based on how long their associated sessions have been inactive.
For example, you may want to review sessions that have been inactive for several days rather than removing recently completed work.
You can also filter by workspace when several projects are involved.
Use the filter to narrow the list before selecting worktrees.
Step 4: Review the Worktree Details
The cleanup editor provides information about the worktrees selected for review.
Depending on the available information, the list can include:
Field | Purpose |
|---|
Session | Identifies the associated agent session |
Host | Shows the machine containing the worktree |
Branch | Identifies the Git branch |
Path | Shows the worktree directory |
Estimated size | Helps understand the storage that may be reclaimed |
Status | Indicates the current cleanup or availability state |
Pay particular attention to the Host and Path fields.
Two remote machines can have worktrees with identical directory paths. Checking only the path can lead to confusion about which machine contains the files.
Step 5: Select Worktrees for Cleanup
Review the listed worktrees and select the ones you no longer need.
If a worktree should be retained, clear its selection.
Remember that cleanup can include eligible sessions outside the currently opened workspace. Do not assume that the editor only shows worktrees associated with the project currently open in VS Code.
Before proceeding, review any important changes and save open file edits in the selected worktrees.
Step 6: Confirm the Cleanup Operation
Select the cleanup button to review the confirmation screen.
The confirmation includes the selected worktrees and their associated hosts.
Check the host information again, especially when multiple remote machines use similar folder structures.
Once you are satisfied with the selection, choose Clean Up.
VS Code begins the cleanup process and schedules the selected worktrees for removal.
The removal may not happen immediately. Depending on the host state and other session usage, cleanup may remain pending until the required conditions are satisfied.
Understanding Remote Cleanup Failures
Remote worktree cleanup depends on the connection and capabilities of the host.
A cleanup operation may fail or remain pending if the host becomes unavailable, another session is still using the worktree, or Git operations cannot complete.
VS Code provides status and error information in the cleanup editor to help identify the problem.
Remote Host Is Disconnected
A remote worktree cannot be removed while its owning host is unavailable.
For example, suppose a developer selects a worktree on a remote development machine but the SSH connection drops before cleanup begins.
The worktree may remain on the remote host.
Reconnect to the host and refresh the cleanup editor to check its current state.
Do not assume that a disconnected host means the worktree has been removed.
Remote Host Does Not Support Cleanup
Not every connected environment necessarily supports the remote cleanup operation.
If a host is unsupported, its worktrees may appear unavailable for cleanup.
Check the host's supported capabilities and update the remote environment if an updated Agent Host is required.
Another Session Is Using the Worktree
VS Code protects worktrees that are still being used by another session or chat.
For example, two conversations may share the same worktree. Removing the directory while one conversation is still using it could disrupt ongoing work.
Resolve the shared usage first.
After the other session releases the worktree, refresh the cleanup editor and retry if the worktree remains eligible.
Connection Drops During Cleanup
A network interruption can occur after cleanup has started.
In this situation, the client may not immediately know whether the remote operation completed.
Reconnect to the host and refresh the cleanup editor.
Check the worktree's current status and error information before attempting another cleanup operation.
A retry may already be scheduled. Avoid repeatedly triggering cleanup without checking the current state.
Worktree Size Is Unknown
VS Code may not always be able to calculate a worktree's size.
This can happen when a host is unavailable or the storage estimate cannot be collected.
An unknown size does not mean that the worktree is empty.
Review the worktree's status and host information before deciding whether to remove it.
Remote Worktree Cleanup vs. Manual Git Removal
Git provides commands for managing worktrees directly. Developers can also remove worktrees through VS Code's Source Control interface.
However, manual removal and agent-session cleanup have different responsibilities.
Consider the following comparison:
Area | Worktree Cleanup | Manual Git removal |
|---|
Main purpose | Remove inactive agent-managed worktrees | Remove a specific Git worktree |
Agent session handling | Marks eligible sessions as done | Does not manage conversation history |
Uncommitted changes | Commits eligible nonignored changes during cleanup | Requires the developer to handle changes first |
Branch | Retained | Retained unless separately deleted |
Remote support | Supports eligible connected remote Agent Hosts | Depends on the Git and remote environment |
Session restoration | Supported for retained sessions | Not provided by ordinary Git worktree removal |
Best use | Cleaning up multiple inactive agent worktrees | Managing worktrees created and maintained manually |
For agent-managed worktrees, the cleanup editor is generally the better choice because it understands the relationship between sessions, branches, and worktree directories.
For worktrees managed directly through Git, the normal Git removal workflow is more appropriate.
Removing a Manually Managed Worktree
If you created a worktree yourself and no longer need it, you can use the Git command.
First, list the worktrees:
git worktree list
The output identifies the repository's worktree directories and their checked-out branches.
Before removing one, inspect its changes and commit, stash, or copy anything that must be preserved.
Then run:
git worktree remove ../feature-login
Git removes the specified worktree if it can be safely removed.
If the worktree contains changes that prevent normal removal, Git may refuse the operation. Do not immediately force-delete it without reviewing the files.
The force option is:
git worktree remove --force ../feature-login
This can remove a worktree containing changes. It should be used only after confirming that the contents are no longer needed.
Also remember that removing a worktree does not automatically delete its branch.
Understanding the Difference Between Worktree Cleanup and Session Deletion
VS Code provides several ways to manage agent sessions, and they should not be treated as interchangeable.
Marking a session as done or archiving it preserves the conversation history. For a worktree session, the archive process can commit uncommitted changes before removing the worktree directory.
Deleting a session is different. Deletion is permanent, and associated worktree resources may also be removed when they are not shared with another session.
Worktree Cleanup is intended for reviewing and removing multiple inactive or leftover worktrees.
Action | Conversation history | Worktree | Branch |
|---|
Mark session done or archive | Retained | Removed when cleanup succeeds | Retained |
Restore session | Retained | Recreated when applicable | Retained |
Delete session | Permanently removed | Removed when eligible | Git branch is generally retained |
Worktree Cleanup | Retained for listed sessions | Scheduled for removal | Retained |
Manual Git removal | Not managed by Git | Removed | Retained |
The exact result depends on the session type and whether other sessions are sharing the same worktree.
For important work, commit and review changes before deleting a conversation or manually removing its directory.
Configuring Automatic Worktree Cleanup Suggestions
VS Code can suggest worktree cleanup when inactive agent worktrees accumulate.
The suggestion behavior is controlled by an experimental setting:
{
"chat.agentSessions.sessionStorageCleanupSuggestion.enabled": true
}
This setting controls cleanup suggestions in the Agents window.
It does not automatically mean that every inactive worktree will be deleted. It helps surface the cleanup experience when the configured conditions indicate that reviewing stored worktrees may be useful.
The cleanup interface also provides options related to automatic completion and deletion retention for eligible merged sessions.
These settings are separate from manually opening Worktree Cleanup.
Automatic cleanup behavior should be reviewed carefully before enabling it in a team environment. Understand the inactivity thresholds, retention period, and conditions under which a worktree can be removed.
Also note that cleanup suggestions do not monitor free disk space on remote hosts. A remote machine can run low on storage even when the local developer's computer has plenty of available space.
Best Practices for Managing Remote Worktrees
Remote worktree cleanup should be part of the development team's regular maintenance process, especially when multiple agents work in parallel.
Consider the following practices:
Review inactive worktrees regularly instead of waiting for remote storage to become full.
Check the owning host before selecting a worktree for removal.
Review branch changes before confirming cleanup.
Preserve important ignored files such as local configuration and test data.
Keep branches until their changes have been reviewed and integrated.
Avoid cleaning up worktrees that are shared by active sessions.
Reconnect to unavailable hosts before retrying cleanup.
Refresh the cleanup editor after a remote connection failure.
Use automatic cleanup settings only after understanding their retention behavior.
Keep important work in Git rather than relying only on local worktree files.
Monitor storage on remote machines independently of local disk usage.
For larger teams, it is also useful to define a consistent branch naming convention for agent sessions. Clear branch names make it easier to identify which worktree belongs to which task.
Advantages and Limitations
The remote Worktree Cleanup experience makes it easier to manage storage across multiple development environments.
Its main advantages include:
Centralized review of inactive agent-managed worktrees.
Cleanup of eligible worktrees on supported remote hosts.
Visibility into the host, branch, path, and estimated size.
Preservation of nonignored changes through commits.
Retention of branches and session history.
Protection for worktrees that are still in use.
Reduced need to manually connect to remote machines and delete directories.
There are also limitations.
Remote cleanup requires a connected host that supports the operation. Cleanup may remain pending when a host becomes unavailable or another session is using the worktree.
Ignored files are deleted when the worktree directory is removed, so local configuration and generated data need separate protection.
The feature also does not replace Git branch management. Developers remain responsible for reviewing, merging, and deleting branches when appropriate.
Summary
Git worktrees help developers and AI agents work on multiple tasks without interfering with the primary working directory. However, the directories created by completed sessions can consume significant storage, especially when agents run on remote development machines.
Visual Studio Code's Worktree Cleanup experience provides a centralized way to review and remove inactive agent-managed worktrees, including eligible worktrees on supported remote Agent Hosts.
The cleanup process preserves branches and session history, commits eligible nonignored changes, and schedules the selected working directories for removal. Developers should still review ignored files, confirm the owning host, and check the status of any pending or failed cleanup operation.
For teams using VS Code agents across local and remote environments, regular worktree cleanup is a useful part of keeping development infrastructure organized and storage usage under control.
Join the conversation! Your thoughts help the community grow.