# remove\_unowned\_worktrees

Remove worktrees that no agent or task owns, such as those `list_worktrees` marks unowned.

writes · idempotent · destructive · MCP tool · [Worktree](https://komrad.dev/latest/api/worktree.md)

Branches are never deleted.

## Calling it

```bash
curl -X POST http://127.0.0.1:8787/komrad.worktree.v1.WorktreeService/RemoveUnownedWorktrees \
  -H 'Content-Type: application/json' \
  -d '{
  "dirs": [
    "<dirs>"
  ]
}'
```

```bash
komrad rpc remove-unowned-worktrees \
  --dirs '<string>'
```

```json
{
  "name": "remove_unowned_worktrees",
  "arguments": {
    "dirs": [
      "<dirs>"
    ]
  }
}
```

Offered by the komrad MCP server to orchestrator agents.

## Request

### RemoveUnownedWorktreesRequest

| Field | Type | Description |
| --- | --- | --- |
| [`dirs`](https://komrad.dev/latest/api/worktree/remove-unowned-worktrees.md#komrad-worktree-v1-RemoveUnownedWorktreesRequest-dirs) | string\[\]\* at most 100 items no repeats each not empty | The worktrees to remove, by the paths [`list_worktrees`](https://komrad.dev/latest/api/worktree/list-worktrees.md) returns. |

## Response

### RemoveUnownedWorktreesResponse

| Field | Type | Description |
| --- | --- | --- |
| [`removed`](https://komrad.dev/latest/api/worktree/remove-unowned-worktrees.md#komrad-worktree-v1-RemoveUnownedWorktreesResponse-removed) | string\[\] | The worktrees removed. |
| [`refused`](https://komrad.dev/latest/api/worktree/remove-unowned-worktrees.md#komrad-worktree-v1-RemoveUnownedWorktreesResponse-refused) | [Refusal\[\]](https://komrad.dev/latest/api/worktree/remove-unowned-worktrees.md#komrad-worktree-v1-Refusal) | The worktrees kept, each with the reason. |

## Types

### Refusal

Refusal is a worktree komrad would not remove, and why.

| Field | Type | Description |
| --- | --- | --- |
| [`dir`](https://komrad.dev/latest/api/worktree/remove-unowned-worktrees.md#komrad-worktree-v1-Refusal-dir) | string | The worktree's path. |
| [`reason`](https://komrad.dev/latest/api/worktree/remove-unowned-worktrees.md#komrad-worktree-v1-Refusal-reason) | [RefusalReason](https://komrad.dev/latest/api/worktree/remove-unowned-worktrees.md#komrad-worktree-v1-RefusalReason) | Why the worktree was kept. |
| [`task_slug`](https://komrad.dev/latest/api/worktree/remove-unowned-worktrees.md#komrad-worktree-v1-Refusal-task_slug) | string | The task the reason names. Empty unless the reason is about a task. |
| [`agent_name`](https://komrad.dev/latest/api/worktree/remove-unowned-worktrees.md#komrad-worktree-v1-Refusal-agent_name) | string | The agent the reason names. Empty unless the reason is about an agent. |
| [`nested_dir`](https://komrad.dev/latest/api/worktree/remove-unowned-worktrees.md#komrad-worktree-v1-Refusal-nested_dir) | string | The worktree inside this one. Empty unless the reason is `nests_worktree`. |
| [`error`](https://komrad.dev/latest/api/worktree/remove-unowned-worktrees.md#komrad-worktree-v1-Refusal-error) | string | What failed. Empty unless a step broke. |

### RefusalReason

RefusalReason is why komrad kept a worktree it was asked to remove.

| Value | Description |
| --- | --- |
| [`outside`](https://komrad.dev/latest/api/worktree/remove-unowned-worktrees.md#komrad-worktree-v1-RefusalReason-outside) | The path is not under the worktree directories. |
| [`unlisted`](https://komrad.dev/latest/api/worktree/remove-unowned-worktrees.md#komrad-worktree-v1-RefusalReason-unlisted) | The listing of the worktree directories does not hold the path. |
| [`task_holds`](https://komrad.dev/latest/api/worktree/remove-unowned-worktrees.md#komrad-worktree-v1-RefusalReason-task_holds) | The task `task_slug` names owns the worktree. Deleting the task removes it. |
| [`task_live`](https://komrad.dev/latest/api/worktree/remove-unowned-worktrees.md#komrad-worktree-v1-RefusalReason-task_live) | The task `task_slug` names is live. Archiving it comes before deleting its worktree. |
| [`task_deleting`](https://komrad.dev/latest/api/worktree/remove-unowned-worktrees.md#komrad-worktree-v1-RefusalReason-task_deleting) | The task `task_slug` names is being deleted, which removes the worktree. |
| [`agent_inside`](https://komrad.dev/latest/api/worktree/remove-unowned-worktrees.md#komrad-worktree-v1-RefusalReason-agent_inside) | The agent `agent_name` names is working in the worktree. |
| [`agent_beneath`](https://komrad.dev/latest/api/worktree/remove-unowned-worktrees.md#komrad-worktree-v1-RefusalReason-agent_beneath) | The agent `agent_name` names is working somewhere under the worktree. |
| [`nests_worktree`](https://komrad.dev/latest/api/worktree/remove-unowned-worktrees.md#komrad-worktree-v1-RefusalReason-nests_worktree) | The worktree `nested_dir` names sits inside this one. Removing that worktree comes first. |
| [`unresolved`](https://komrad.dev/latest/api/worktree/remove-unowned-worktrees.md#komrad-worktree-v1-RefusalReason-unresolved) | Komrad could not resolve the path. `error` says why. |
| [`list_failed`](https://komrad.dev/latest/api/worktree/remove-unowned-worktrees.md#komrad-worktree-v1-RefusalReason-list_failed) | Git could not list the worktrees of the worktree's repository. `error` says why. |
| [`unlock_failed`](https://komrad.dev/latest/api/worktree/remove-unowned-worktrees.md#komrad-worktree-v1-RefusalReason-unlock_failed) | Git could not unlock the worktree. `error` says why. |
| [`remove_failed`](https://komrad.dev/latest/api/worktree/remove-unowned-worktrees.md#komrad-worktree-v1-RefusalReason-remove_failed) | Komrad could not remove the worktree's directory. `error` says why. |
