Stumbled onto this question, here's how I did it, it's not exactly automated but this should be a start.

# Step 1: Delete local branches
1. Get list of local branches
```bash
$ git --no-pager branch --sort=-committerdate > branches_to_delete.txt 
```

2. Delete the rows not to be deleted in file `branches_to_delete.txt`

3. delete the branches at local
```bash
$ cat branches_to_delete.txt | xargs git branch -D
```

# Step 2: Delete remote branches
1. Get list of remote branches
```bash
$ git remote prune origin # delete all the local refs to already-deleted remote branches
$ git --no-pager branch -r --sort=-committerdate > branches_to_delete.txt
```

2. delete branches not to be deleted in file `branches_to_delete.txt`

3. replace all "origin/" with "" (vim, emacs, vscode, sed, etc.) in file

4. delete the branches at remote:
```bash
$ cat branches_to_delete.txt | xargs git push origin --delete
```