Bulk delete merged git branches

When working with features on it, it’s a good idea to delete branches that are already merged to our main branch to keep it clean, but sometimes we forget to do that, and our VC system becomes polluted with merged branches nobody has used for years.

Here is a simple tutorial on how to find and delete all such branches using CLI.

# Make sure remotes are up to date (with stale remotes purged):

git fetch -p

# Initial no-op run --- do the branches to delete look correct?

# Be careful to omit 'master' from the output.

git branch --remote --merged origin/master | grep -v 'master' | cut -b 10- | xargs

# Do the bulk delete!!! (can take a long time...)

git branch --remote --merged origin/master | grep -v 'master' | cut -b 10- | xargs git push --delete origin