Search code examples
githubdevopsgithub-actionspull-request

How to auto delete branches if PR is closed?


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


Solution

  • 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}}