EDIT: I saw your other comment (incorrectly) claiming that git remote prune origin will do the job. I don't know of any git trickery that can do the job. And believe me I'd love it if there was something.
That looks fine and will generally work, except when the current branch is one of the ones to be deleted.
The script in OP handles that edge case by switching to the main branch before deleting. That may or may not be intuitive but I think it's a reasonable response. The script also prints error messages for various other bad inputs. Once you strip all that out it's only a few lines long.
I'm going to assume you have about as many years of experience as me based on your profile, but if you're going to argue that maintaining this script - for the sole convenience of deleting a branch out from underneath you - is worth it, then there's nothing more I can do to help you.
I have an alias "git prune-branches" that deletes branches that remote has deleted. Is that what you are loking for? Cannot check the code right now but will try to get back tomorrow.
EDIT: I saw your other comment (incorrectly) claiming that git remote prune origin will do the job. I don't know of any git trickery that can do the job. And believe me I'd love it if there was something.