I’ve been using something similar for years with Nushell.
git branch | lines | where ($it !~ '^*') | each {|br| git branch -D ($br | str trim)} | str trim