# Delete worktree directory before git metadata cleanup during archive \(\#53959\) · gitcafe/zed

[View on GitCafe](https://git.cafe/gitcafe/zed/commit/a30bc615e2d00c047a32c9e9ecb412ed704434f9)

Repository: [gitcafe/zed](https://git.cafe/gitcafe/zed)

Visibility: public

Requested revision: a30bc615e2d00c047a32c9e9ecb412ed704434f9

Requested commit: a30bc615e2d00c047a32c9e9ecb412ed704434f9

Commit: a30bc615e2d00c047a32c9e9ecb412ed704434f9

Tree: 593d3d4ac7e9a45258021c85e82ff141457d8eb8

Author: Richard Feldman

Committer: GitHub

## Message

```
Delete worktree directory before git metadata cleanup during archive (#53959)

When archiving a thread's linked worktree, `git worktree remove` can
fail to delete the working directory while still removing the admin
metadata in `.git/worktrees/<name>`. This leaves an orphaned folder on
disk that no longer appears in `git worktree list`.

## Fix

Delete the directory ourselves first with `std::fs::remove_dir_all`,
then call `git worktree remove --force` to clean up only the admin
entry. Git already handles the case where the working tree is already
gone (since
[git/git@ee6763a](https://github.com/git/git/commit/ee6763af0a3b97225803c6c908a29de40336cf38)).

Also fixes `FakeGitRepository::remove_worktree` to handle a missing
working tree directory (matching real git behavior) by scanning
`.git/worktrees/` entries when the `.git` pointer file can't be read.

Release Notes:

- Fixed archiving an agent thread sometimes leaving the worktree folder
on disk even though the git worktree was removed.
```

## Parents

- [848c3da99e40335ea93cf3951cfd3456cc7f3994](https://git.cafe/gitcafe/zed/commit/848c3da99e40335ea93cf3951cfd3456cc7f3994?format=markdown)

[Source at this commit](https://git.cafe/gitcafe/zed/tree/a30bc615e2d00c047a32c9e9ecb412ed704434f9?format=markdown)
