After you merge a branch into main, you should delete it to keep your repository clean. GitHub shows a "Delete branch" button on the pull request page right after merging. You can also delete branches from the Branches page: click the "X branches" link on your repo, find the branch, and click the trash can icon. Deleting a branch does not delete any merged code — all the changes are safely in main.
Why and When to Delete Branches on GitHub
Branches are meant to be temporary. You create one to work on a feature or fix, merge it into main when it is ready, and then delete it. If you never delete old branches, your repository accumulates dozens or hundreds of stale branches that make it hard to find the active ones. Think of it like cleaning up after cooking — the meal is done (your code is merged), so you wash the dishes (delete the branch). Deleting a merged branch is completely safe because all the changes from that branch are already part of main. The code, the commits, and the full history are preserved. If you are using AI tools like Lovable or Replit that connect to GitHub, keeping a clean branch list prevents confusion about which branch contains the latest code. GitHub even offers an automatic setting that deletes branches immediately after they are merged.
Prerequisites
- A free GitHub account (sign up at github.com)
- A repository with at least one branch that has been merged (or one you want to remove)
- Write access to the repository
Step-by-step guide
Delete a branch from the merged pull request page
Delete a branch from the merged pull request page
After you merge a pull request, GitHub shows a purple "Merged" banner at the bottom of the PR page along with a message like "Pull request successfully merged and closed." Directly below that, you see a "Delete branch" button. Click it. The button changes to "Restore branch" in case you want to undo the deletion. This is the quickest way to clean up — do it right after merging.
Expected result: The branch is deleted and the button changes to "Restore branch."
Delete a branch from the Branches page
Delete a branch from the Branches page
If you missed the delete button on the PR page, go to your repository's main page. Find the link that says "X branches" near the top (next to "X commits" and "X tags"). Click it to open the Branches page. This shows all branches in your repo. Find the branch you want to delete — stale branches appear in the "Stale" tab. Click the trash can icon on the right side of the branch row. Confirm the deletion if prompted.
Expected result: The branch disappears from the list. You see a notification confirming it was deleted.
Enable automatic branch deletion (optional)
Enable automatic branch deletion (optional)
To avoid manual cleanup, you can set GitHub to automatically delete branches after they are merged. Go to your repository's Settings page (click the "Settings" tab in the top navigation). Scroll down to the "Pull Requests" section. Check the box labeled "Automatically delete head branches." Click Save if needed. From now on, every time a pull request is merged, the source branch is deleted automatically.
Expected result: The "Automatically delete head branches" checkbox is checked. Future merged branches will be deleted automatically.
Restore a deleted branch if needed
Restore a deleted branch if needed
If you accidentally delete a branch, you can restore it. Go to the pull request that was associated with the branch (find it under the "Closed" tab on the Pull requests page). At the bottom of the PR, you will see a "Restore branch" button. Click it to bring the branch back with all its commits intact. You can also find recently deleted branches on the Branches page under the "Deleted" filter.
Expected result: The deleted branch is restored and appears in the branch dropdown again.
Complete working example
1# Repository settings managed via GitHub Settings app2# Install: https://github.com/apps/settings3# This file automates repository configuration45repository:6 name: my-project7 description: A web app built with React and Supabase8 private: true9 has_issues: true10 has_projects: false11 has_wiki: false12 default_branch: main13 allow_squash_merge: true14 allow_merge_commit: false15 allow_rebase_merge: false16 delete_branch_on_merge: true # Auto-delete after merge1718branches:19 - name: main20 protection:21 required_pull_request_reviews:22 required_approving_review_count: 123 required_status_checks: null24 enforce_admins: false25 restrictions: nullCommon mistakes when deleting a Branch in GitHub After Merging
Why it's a problem: Deleting a branch that has not been merged yet
How to avoid: GitHub warns you if the branch has unmerged changes. Always merge first, then delete. If you need to discard the work entirely, that is okay — but make sure you are intentionally abandoning those changes.
Why it's a problem: Thinking that deleting a branch deletes the merged code
How to avoid: Once a branch is merged into main, all its commits become part of main's history. Deleting the branch only removes the branch pointer — the code stays safe.
Why it's a problem: Never deleting branches and accumulating dozens of stale ones
How to avoid: Enable "Automatically delete head branches" in Settings, or make a habit of clicking Delete branch after every merge.
Why it's a problem: Deleting the default branch (main)
How to avoid: GitHub prevents deleting the default branch. If you somehow need to rename it, go to Settings → Branches → Default branch, but this is rarely needed.
Best practices
- Delete branches immediately after merging — make it part of your workflow.
- Enable "Automatically delete head branches" in repository Settings to automate cleanup.
- Review the Branches page periodically to find and delete stale branches.
- Before deleting an unmerged branch, double-check that you truly want to discard the work.
- Use the "Stale" tab on the Branches page to find branches that have not been updated recently.
- Communicate with your team before deleting shared branches that others may be working on.
Still stuck?
Copy one of these prompts to get a personalized, step-by-step explanation.
I have a GitHub repository with 30 old branches that were already merged. What is the safest way to clean them all up without losing any code?
Frequently asked questions
Is it safe to delete a branch after merging?
Yes, completely safe. Once a branch is merged into main, all its commits and changes are part of main's history. The branch is just a label pointing to those commits. Deleting the label does not delete the commits.
Can I recover a deleted branch?
Yes. Go to the associated pull request and click the "Restore branch" button. You can also find recently deleted branches on the Branches page. GitHub keeps deleted branch data for a period of time.
What if I delete a branch that Lovable or V0 is connected to?
Lovable and V0 typically sync with the main branch. If you delete a feature branch, it should not affect your connected tools. However, if you have manually configured a tool to track a specific branch, deleting it will break that connection.
Should I delete branches that were never merged?
Only if you are sure you no longer need the work on that branch. If the changes might be useful later, leave the branch or merge it first. You can always check what is on the branch by switching to it before deleting.
How do I delete multiple branches at once?
GitHub's web interface only lets you delete branches one at a time from the Branches page. For bulk deletion, GitHub Desktop or a team automation tool can help. Services like RapidDev can set up automated branch cleanup workflows for your repository.
Does the default branch (main) ever get deleted?
No. GitHub prevents deletion of the default branch. You can change which branch is the default in Settings → Branches, but the default branch itself cannot be deleted.
Talk to an Expert
Our team has built 600+ apps. Get personalized help with your project.
Book a free consultation