How to Fix “cannot delete branch” Error in Git
If git branch -d or git branch -D prints error: cannot delete branch 'branch-name' used by worktree at 'path', the branch is checked out in some working directory. Switch that working directory to another branch, or clean up that working directory, and the same command will then delete the branch.
This error can appear when you clean up a branch that you no longer need after it has been merged. There are four places where a branch can be in use: the current folder, another folder created with git worktree, a worktree folder that has already been deleted, and an interrupted rebase or bisect. Each one has a different fix.
I tested on Ubuntu 24.04, Git 2.43.0, and bash 5.2. To match the English messages, I ran the commands with LANG=C. I replaced the working directory paths in the output with /path/to/…. I did not test on Windows, macOS, or Git GUI tools.

You cannot delete the current branch even with -D
I tried to delete feature/login while I was still on that branch. Both -d and -D, which forces deletion, stopped with the same message. The exit code was 1.
$ git branch
* feature/login
main
$ git branch -d feature/login
error: cannot delete branch 'feature/login' used by worktree at '/path/to/app'
$ git branch -D feature/login
error: cannot delete branch 'feature/login' used by worktree at '/path/to/app'
The path after at is the working directory you are in now (/path/to/app). The branch marked with * in the git branch list is the one you are on.
According to the git branch reference, -D is short for --delete --force. --force used with -d lets you delete a branch whether or not it is merged. Adding --force does not change the check for whether the branch is in use. In the Git source (builtin/branch.c), before deleting a branch, Git checks whether any working directory is using it. If one is, Git prints this error and ends without deleting the branch.
Fix: switch to another branch, then delete
I switched to main and ran the same command again. The branch was deleted.
$ git switch main
Switched to branch 'main'
$ git branch -d feature/login
Deleted branch feature/login (was ddb34d1).
$ git branch
* main
A branch open in another worktree
If you opened a branch in another folder created with git worktree add, you cannot delete it even when the current folder is on a different branch. In this case, the path after at is the path of that other folder.
$ git worktree add ../app-hotfix hotfix
Preparing worktree (checking out 'hotfix')
HEAD is now at ddb34d1 ログイン画面
$ git branch
+ hotfix
* main
$ git branch -d hotfix
error: cannot delete branch 'hotfix' used by worktree at '/path/to/app-hotfix'
A + in the git branch list marks a branch that is open in another working directory. To see which folder has which branch open, use git worktree list.
$ git worktree list
/path/to/app ddb34d1 [main]
/path/to/app-hotfix ddb34d1 [hotfix]
I cover how to use worktrees in a separate article: "How to open different branches in different folders at the same time with git worktree" (in Japanese).
Fix 1: leave the branch in that folder
If you still need that folder, switch it to another branch. If there is no other branch to switch to, you can use git switch with --detach. This puts the folder on a commit instead of a branch (detached HEAD). With -C you can point at the folder and run the command without cd.
$ git -C ../app-hotfix switch --detach
HEAD is now at ddb34d1 ログイン画面
$ git branch -d hotfix
Deleted branch hotfix (was ddb34d1).
$ git worktree list
/path/to/app ddb34d1 [main]
/path/to/app-hotfix ddb34d1 (detached HEAD)
Fix 2: remove the folder
If you no longer need the folder either, remove it with git worktree remove, then delete the branch. After the removal, the same git branch -d worked.
$ git branch -d hotfix2
error: cannot delete branch 'hotfix2' used by worktree at '/path/to/app-hotfix2'
$ git worktree remove ../app-hotfix2
$ git branch -d hotfix2
Deleted branch hotfix2 (was ddb34d1).
According to the git worktree reference, git worktree remove does not delete a folder that still contains modified files or files Git does not track. Adding --force removes such folders too.
The branch still shows as in use after the folder was deleted
If you delete a worktree folder directly with rm -rf or a similar command, Git still keeps a record that the branch was open in that folder. In this state, I still could not delete the branch. The path after at is a folder that no longer exists.
$ rm -rf ../app-exp
$ git worktree list
/path/to/app ddb34d1 [main]
/path/to/app-exp ddb34d1 [experiment] prunable
$ git branch -d experiment
error: cannot delete branch 'experiment' used by worktree at '/path/to/app-exp'
A worktree marked prunable in git worktree list is one whose folder can no longer be found. According to the git worktree reference, worktrees that git worktree prune can clean up are shown as prunable.
With --dry-run, nothing is deleted and Git only shows what would be removed. After I checked the output and ran prune, the record was removed and I could delete the branch.
$ git worktree prune --dry-run --verbose
Removing worktrees/app-exp: gitdir file points to non-existent location
$ git worktree prune
$ git worktree list
/path/to/app ddb34d1 [main]
$ git branch -d experiment
Deleted branch experiment (was ddb34d1).
A branch in the middle of a rebase or bisect
While a rebase is stopped by a conflict, git branch --show-current prints nothing. It looks as if you are not on any branch, but when I tried to delete the branch being rebased, Git stopped because the current folder was using it.
$ git rebase main topic
Rebasing (1/1)
Auto-merging a.txt
CONFLICT (add/add): Merge conflict in a.txt
error: could not apply ecd5e23... topic のコミット
(hint: の行と、最後の Could not apply の行は省略)
$ git branch --show-current
$ git branch
* (no branch, rebasing topic)
main
topic
$ git branch -D topic
error: cannot delete branch 'topic' used by worktree at '/path/to/app'
The first line of the git branch list shows (no branch, rebasing topic), which tells you that topic is being rebased. In the Git source (branch.c), if a working directory is in the middle of a rebase or bisect, the branch being operated on is added to the list of branches in use.
git rebase --abort cancels the rebase and returns you to the original branch. After I ran it, I was back on topic, so I switched to main and deleted it.
$ git rebase --abort
$ git branch --show-current
topic
$ git switch main
Switched to branch 'main'
$ git branch -D topic
Deleted branch topic (was ecd5e23).
A bisect in progress behaves the same way. When the list shows (no branch, bisect started on search) and I tried to delete search, I got the same error. As the git bisect reference describes, git bisect reset ends the bisect and returns you to the original branch, so you can switch to another branch and then delete it.
$ git branch
* (no branch, bisect started on search)
main
search
$ git branch -D search
error: cannot delete branch 'search' used by worktree at '/path/to/app'
$ git bisect reset
Previous HEAD position was 022b175 search 2
Switched to branch 'search'
$ git switch main
Switched to branch 'main'
$ git branch -D search
Deleted branch search (was 3023756).
If you see "not fully merged", the cause is different
Another error you can get when deleting a branch is not fully merged. This one does not come from the branch being in use. Instead, -d stops the deletion because the branch has commits that have not been merged.
$ git branch -d draft
error: the branch 'draft' is not fully merged.
If you are sure you want to delete it, run 'git branch -D draft'
$ git branch -D draft
Deleted branch draft (was 611eca4).
As the message says, using -D deleted the branch. The branch draft had commits that existed nowhere else, so before you use -D, check that those commits are not needed. I cover the difference between -d and -D, and how to recreate a deleted branch, in a separate article: "How to rename and delete branches in Git" (in Japanese).
Switching from -d to -D does not fix cannot delete branch. Switch to another branch in the place where the branch is in use, or clean up the working directory or its record, and then delete the branch.





