{"record":{"id":"5b91e60a377f40cc","repo":"theonedev/onedev","slug":"cannot-delete-this-branch-as-it-has-workspaces","errorCode":null,"errorMessage":"Cannot delete this branch as it has workspaces","messagePattern":"Cannot delete this branch as it has workspaces","errorType":"http","errorClass":"NotAcceptableException","httpStatus":406,"severity":"warning","filePath":"server-core/src/main/java/io/onedev/server/service/impl/DefaultProjectService.java","lineNumber":973,"sourceCode":"\t@Transactional\n\t@Override\n\tpublic void onDeleteBranch(Project project, String branchName) {\n\t\tfor (Iterator<BranchProtection> it = project.getBranchProtections().iterator(); it.hasNext(); ) {\n\t\t\tBranchProtection protection = it.next();\n\t\t\tPatternSet patternSet = PatternSet.parse(protection.getBranches());\n\t\t\tpatternSet.getIncludes().remove(branchName);\n\t\t\tpatternSet.getExcludes().remove(branchName);\n\t\t\tprotection.setBranches(patternSet.toString());\n\t\t\tif (protection.getBranches().length() == 0)\n\t\t\t\tit.remove();\n\t\t}\n\t}\n\n\t@Transactional\n\t@Override\n\tpublic void deleteBranch(Project project, String branchName) {\n\t\tif (workspaceService.count(project, branchName) > 0) \n\t\t\tthrow new NotAcceptableException(\"Cannot delete this branch as it has workspaces\");\n\t\tonDeleteBranch(project, branchName);\n\t\tgitService.deleteBranch(project, branchName);\n\t}\n\n\t@Transactional\n\t@Override\n\tpublic void onDeleteTag(Project project, String tagName) {\n\t\tfor (Iterator<TagProtection> it = project.getTagProtections().iterator(); it.hasNext(); ) {\n\t\t\tTagProtection protection = it.next();\n\t\t\tPatternSet patternSet = PatternSet.parse(protection.getTags());\n\t\t\tpatternSet.getIncludes().remove(tagName);\n\t\t\tpatternSet.getExcludes().remove(tagName);\n\t\t\tprotection.setTags(patternSet.toString());\n\t\t\tif (protection.getTags().length() == 0)\n\t\t\t\tit.remove();\n\t\t}\n\t}\n","sourceCodeStart":955,"sourceCodeEnd":991,"githubUrl":"https://github.com/theonedev/onedev/blob/d44925c47c37992c828ea673a5f9620539bc3ff2/server-core/src/main/java/io/onedev/server/service/impl/DefaultProjectService.java#L955-L991","documentation":"OneDev throws this NotAcceptableException when attempting to delete a git branch that still has one or more dev workspaces opened against it. DefaultProjectService.deleteBranch first checks workspaceService.count(project, branchName); a non-zero count means users still have active workspaces based on that branch, so the deletion is refused to avoid breaking those workspaces.","triggerScenarios":"Calling ProjectService.deleteBranch(project, branchName) (via UI branch list, REST API, or code) while any user has an active workspace checked out from that branch.","commonSituations":"Cleaning up stale branches before realizing teammates still have workspaces on them; deleting a feature branch from a script while a CI/user workspace session remains attached; forgetting to close/terminate workspaces after finishing work on the branch.","solutions":["Close or terminate all workspaces associated with the branch (Project -> Workspaces, or delete them via API), then retry the branch deletion.","Identify workspaces with workspaceService listing/API and remove those referencing the branch.","Wait for users to finish and close their workspaces, then delete the branch.","If the branch must go now, re-create the affected users' workspaces on another branch first."],"exampleFix":"// before\nprojectService.deleteBranch(project, \"feature-x\"); // throws: has workspaces\n// after: delete workspaces on the branch first\nfor (var ws : workspaceService.findAll(project)) {\n    if (\"feature-x\".equals(ws.getBranch()))\n        workspaceService.delete(ws);\n}\nprojectService.deleteBranch(project, \"feature-x\");","handlingStrategy":"validation","validationCode":"// Check for attached workspaces before deleting the branch\nlong wsCount = workspaceService.count(project, branchName);\nif (wsCount > 0) {\n    // delete or reassign the \" + wsCount + \" workspaces first\n    return;\n}\nprojectService.deleteBranch(project, branchName);","typeGuard":"function branchIsDeletable(project, branchName, workspaceService) {\n  return workspaceService.count(project, branchName) === 0;\n}","tryCatchPattern":"try {\n    projectService.deleteBranch(project, branchName);\n} catch (NotAcceptableException e) {\n    logger.info(\"Branch {} kept: {}\", branchName, e.getMessage());\n    // notify workspace owners or close workspaces then retry\n}","preventionTips":["List workspaces per branch before any branch cleanup","Adopt a policy of closing workspaces when a feature branch is merged","Automate workspace cleanup as part of branch-deletion scripts","Communicate with teammates before deleting shared branches"],"tags":["branch","workspace","git","precondition"],"backgroundTag":"invalid-state-transition","analyzedSha":"d44925c47c37992c828ea673a5f9620539bc3ff2","analyzedAt":"2026-09-06T07:18:27.995Z","contentChangedAt":"2026-09-06T07:18:27.995Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}