I found a solution (which isn’t mentioned in the docs anywhere):

curl -s -X DELETE -u username:${{secrets.GITHUB_TOKEN}} https://api.github.com/repos/${{ github.repository }}/git/refs/heads/${{ github.head_ref }}
Discussions

Missing API for "Automatically delete head branches"
You switched accounts on another tab or window. Reload to refresh your session. ... There was an error while loading. Please reload this page. Something went wrong. There was an error while loading. Please reload this page. ... There is no REST API for the “Automatically delete head branches” ... More on github.com
🌐 github.com
2
2
Add a Branch.delete() method
If you're iterating through a list of branches by name, it would be handy to be able to delete them directly: # Example 1 repo.get_branch('feature-branch-1').delete() # Example 2 for br... More on github.com
🌐 github.com
3
October 15, 2019
How to delete a GitHub repo using the API - Stack Overflow
I am getting familiar with the GitHub API http://developer.github.com/v3/ I am trying things out both with RESTClient plugin for Firefox and with curl command line tool. I have found out how to cr... More on stackoverflow.com
🌐 stackoverflow.com
Auto-delete branch on pull request merge
It would be nice if we could have ... having to revisit the PR and manually click the "Delete branch" button, the branch for the pull request would be deleted automatically. This is one of the features @jessfraz was just tweeting about running out of GitHub API calls to implement ... More on github.com
🌐 github.com
40
November 13, 2017
🌐
GitHub
github.com › PyGithub › PyGithub › issues › 1260
Add a Branch.delete() method · Issue #1260 · PyGithub/PyGithub
October 15, 2019 - # Example 1 repo.get_branch('f... available to delete branches is to call repo.get_git_ref() to get a reference to the branch, and then call .delete() on the returned object....
Author   nchammas
Top answer
1 of 2
29

If you created the token you're using through the Applications page, then this token will have these scopes: user, public_repo, repo, gist. You can verify this by making an API request with that token and looking at the response HTTP headers:

curl -v -H 'Authorization: token xxx' https://api.github.com

Look for the X-OAuth-Scopes response header which will have the list of scopes:

X-OAuth-Scopes: user, public_repo, repo, gist

However, to delete a repository, the token needs to have the delete_repo scope.

So, you need a token that has different scopes than the one you have. You can create such a token using the Authorizations API:

curl -v -u username -X POST https://api.github.com/authorizations -d '{"scopes":["delete_repo"], "note":"token with delete repo scope"}'

This will return a JSON document with the new token which you should be able to use to delete a repository:

{
  "id": XXXXX,
  "url": "https://api.github.com/authorizations/XXXXX",
  "app": {
    "name": "GitHub API",
    "url": "http://developer.github.com/v3/oauth/#oauth-authorizations-api",
    "client_id": "00000000000000000000"
  },
  "token": "XXXXXX",
  "note": "token with delete repo scope",
  "note_url": null,
  "created_at": "2013-10-11T20:34:49Z",
  "updated_at": "2013-10-11T20:34:49Z",
  "scopes": [
    "delete_repo"
  ]
}

Of course, when creating a token this way, you can ask for multiple scopes, not just the delete_repo scope.

Also, as a side-note, the reason why the API is returning a 404 error when you don't have the right authorization is to prevent information leakage.

2 of 2
20

To delete a GitHub repo:

curl \
  -X DELETE \
  -H "Accept: application/vnd.github.v3+json" \
  -H "Authorization: token ${token}" \
   https://api.github.com/repos/${username}/${reponame}

Define or replace ${token}, ${username}, and ${reponame}. The token must have access to the delete_repo scope.

Find elsewhere
🌐
Josh-Ops
josh-ops.com › posts › github-delete-branch-protection-rules
Delete GitHub Branch Protection Rules Programmatically | josh-ops
December 19, 2023 - After a migration, or maybe when doing cleanup, you may want to delete branch protection rules in bulk. Instead of having to click through each branch protection rule individually, I wrote a PowerShell script that leverages the GraphQL endpoint. At the time I wrote this a few years ago, there wasn’t an API for deleting branch protection rules, only GraphQL.
🌐
GitHub
github.com › isaacs › github › issues › 1121
Auto-delete branch on pull request merge · Issue #1121 · isaacs/github
November 13, 2017 - It would be nice if we could have ... having to revisit the PR and manually click the "Delete branch" button, the branch for the pull request would be deleted automatically. This is one of the features @jessfraz was just tweeting about running out of GitHub API calls to implement ...
Author   stuartpb
🌐
GitHub
github.com › orgs › community › discussions › 49570
PR API: add a "delete_branch_on_merge" option in body · community · Discussion #49570
March 8, 2023 - Thank you ;) However, this will just auto-delete all branches after each PR merge ... which is not what I want I'd like to only auto-delete branches when I want it. note: I know, all I have to do via API is another call to delete the branch after the merge, which is what I'm doing now :)
🌐
Geekflare
geekflare.com › home › development › how to delete github branch?
How to Delete GitHub Branch?
August 27, 2021 - That’s it; we have successfully deleted a branch both locally and remotely. There’s a slightly different way to do it using the GitHub web app.
🌐
GitHub
docs.github.com › en › rest › branches › branch-protection
REST API endpoints for protected branches - GitHub Docs
Removing admin enforcement requires ... and branch protection to be enabled. This endpoint works with the following fine-grained token types: ... curl -L \ -X DELETE \ -H "Accept: application/vnd.github+json" \ -H "Authorization: Bearer <YOUR-TOKEN>" \ -H "X-GitHub-Api-Version: ...
🌐
Mkavana
mkavana.github.io › docs › branches › delete-branch.html
WayPoint Ventures : Delete branch
Information about the Git delete branch command is available from the Software Freedom Conservancy’s Git branch documentation. ... Complete the following steps to delete a branch using VSC.
🌐
GitHub
github.com › ScoopInstaller › Main › discussions › 4696
Automatically Delete Fork Branch After PR Merged in Upstream Repository · ScoopInstaller/Main · Discussion #4696
I don't think this is possible, unless GitHub itself provides a way to automatically delete branches in the fork's Settings page. You can set up a webhook server and listen to merge events from your fork, then call the GitHub API to delete the branches, but that's too much work IMO.
Author   ScoopInstaller
🌐
Apiary Help
help.apiary.io › tools › github-integration
GitHub Integration | Apiary Help
If you want to delete a branch in Apiary, switch to the branch from the editor, go to the API Project settings for that branch and delete your branch from there. ... If you delete branch in GitHub, and then try to push to it from Apiary, it ...
🌐
GitHub
github.blog › home › changelogs › delete organizations with rest api
Delete Organizations with REST API - GitHub Changelog
March 22, 2025 - GitHub enterprise and organization owners can now use a REST API to delete their organizations and all corresponding repositories. Organization names will be locked for 90 days following the organization…
🌐
GitHub
docs.github.com › en › rest › branches › branches
REST API endpoints for branches - GitHub Docs
[ { "name": "master", "commit": { "sha": "c5b97d5ae6c19d5c5df71a34c7fbeeda2479ccbc", "url": "https://api.github.com/repos/octocat/Hello-World/commits/c5b97d5ae6c19d5c5df71a34c7fbeeda2479ccbc" }, "protected": true, "protection": { "required_status_checks": { "enforcement_level": "non_admins", "contexts": [ "ci-test", "linter" ] } }, "protection_url": "https://api.github.com/repos/octocat/hello-world/branches/master/protection" } ] This endpoint works with the following fine-grained token types:
🌐
GitHub
github.com › neondatabase › delete-branch-action
GitHub - neondatabase/delete-branch-action · GitHub
It's designed for workflows where you need to programmatically remove Neon branches, such as in cleanup processes after testing. Using the action requires adding a Neon API key to your GitHub Secrets.
Starred by 8 users
Forked by 5 users
🌐
GitHub
github.com › marketplace › actions › delete-merged-branch
Delete merged branch · Actions · GitHub Marketplace
This app is compatible with GitHub Actions. You need to create a workflow that is triggered on the pull_request event for this. Then, you use this repo for the action. (SvanBoxel/delete-merged-branch@master). Don't forget to check the GITHUB_TOKEN secret.
🌐
GitHub
github.blog › home › changelogs › delete a branch with an open pull request
Delete a branch with an open pull request - GitHub Changelog
March 22, 2025 - Previously, GitHub's web UI did not allow deleting a branch that was associated with an open pull request. Now you can delete such a branch from the UI. However, doing so will close all open pull requests associated with the branch.