#TIL 43 - Delete all remote branches except main | ProductsWay

#TIL 43 - Delete all remote branches except main

Jan 13, 2024 · Dung Huynh

What

Delete all remote branches on origin except the one you want to keep (main).

Why

Merged feature branches linger on origin after local cleanup. Prune stale tracking refs, list only refs under origin, and exclude the exact HEAD and main names before deleting anything. Preview the list first because remote deletion affects everyone using the repository.

How

git remote prune origin
git for-each-ref --format='%(refname:strip=3)' refs/remotes/origin \
  | grep -Ev '^(HEAD|main)$'

After you verify the preview, run the same exact filter and delete each branch:

git for-each-ref --format='%(refname:strip=3)' refs/remotes/origin \
  | grep -Ev '^(HEAD|main)$' \
  | while IFS= read -r branch; do git push origin --delete "$branch"; done