This exact version is untested and a hand-edit from a very specific use-case I have. Take backups and use at your own risk!

The only problem is that {taxonomy_term_data} doesn't have a created column, so this will immediately delete orphaned terms. It also doesn't take into term hierarchy, or tags being on entities other than nodes.