Same using a git alias rather than shell, and without the network bits, it just cleans up branches which have an upstream that has been deleted:
'!f() { git branch --format '%(refname:short) %(upstream:track,nobracket)' | awk '$2~/^gone$/{print $1}' | xargs git branch -D; }; f'