How To Remove Orphaned Tags

When experimenting and testing with WordPress taxonomies sometimes you end up with a lot of junk in the database that wouldn’t look good on a production site or even demoing to friends. In my situation, a lot of foobar tags were used during the testing of my Savory Bookmarks plugin.

A quick search revealed an SQL way of deleting orphaned tags — those with counts of zero that should not be associated with any objects such as posts, pages and custom post types as long as the WordPress taxonomy API was used. Here’s what the query looks like:

delete FROM wp_terms WHERE term_id IN (SELECT term_id FROM wp_term_taxonomy WHERE count = 0 )

Leave a Reply