1

I know we can auto delete branches when PR gets merged on Github. Is there a way we can get branches to auto delete when PR is closed?

I do not see an option in Settings tab of Repo for deleting branches when PR gets closed

dtandon
  • 108
  • 5
  • 1
    You could use an action from the marketplace ([here is one](https://github.com/dawidd6/action-delete-branch)) that delete branch. Then you could implement a workflow using this action, that would trigger only when a PR is closed, extracting the branch name automatically by using the github context variable in pull_request for the branch name which is `${{ github.head_ref }}`. – GuiFalourd Nov 30 '22 at 12:11
  • Thanks @GuiFalourd. I have already implemented the github action solution. I was wondering if there is a simpler solution out there. Thanks a lot for your reply – dtandon Dec 01 '22 at 05:18
  • Does this answer your question? [Automatically delete git branch after merge to master](https://stackoverflow.com/questions/14485936/automatically-delete-git-branch-after-merge-to-master) – ThisaruG Dec 01 '22 at 09:55
  • Hey @ThisaruG, no. I want to delete the branch when PR gets closed, not merged – dtandon Dec 01 '22 at 11:58

3 Answers3

1

While completing the PR, we have a Checkbox, when selected will delete the source branch as soon as we complete the PR.

Complete the PR by clicking on Complete

As soon as you click on Complete, there is a Checkbox for Deleting the source branch. Select the respective Checkbox, and Click on Complete to complete the PR and for deleting the SOURCE branch. enter image description here

sunbogaa
  • 405
  • 2
  • 6
  • 12
1

As there is no inbuilt functionality for this on Github, I used github actions for this. Here's a snippet on how we can get this done:

 steps:
      - name: Delete closed PR branch
        uses: dawidd6/action-delete-branch@v3
        with:
          github_token: ${{github.token}}
          numbers: ${{github.event.pull_request.number}}
dtandon
  • 108
  • 5
0

Option will be showed whenever you merge the code from the PR to the target branch.

Ivneet Singh
  • 356
  • 2
  • 5