Few days ago, I deleted a lot of dump posts from one of my WordPress site. After that, I recognized there were ton of unused tags existed in the database. It’s wasting the server resources and slow down my site by retrieving/looping unnecessary records.
Therefore, I decided to clean up all unused tags to free up all related tables and increase the performance as well as page loading time.
SQL Commands to delete/remove all unused post tags in WordPress
You can use PhpMyAdmin or a particular SQL client to run these commands below. Again, please backup your database first and make sure you understand what are you doing.
DELETE FROM wp_terms WHERE term_id IN (SELECT term_id FROM wp_term_taxonomy WHERE COUNT = 0 ); DELETE FROM wp_term_taxonomy WHERE term_id NOT IN (SELECT term_id FROM wp_terms); DELETE FROM wp_term_relationships WHERE term_taxonomy_id NOT IN (SELECT term_taxonomy_id FROM wp_term_taxonomy); |
I worked on my side and significant reduce the database site. Remember to replace ‘wp_’ with your table_prefix.
After deleted all unused tags and its relationship, you should optimize those tables to reclaim the unused space and to defragment the data file. It’s very useful when you have deleted a large part of a table and definitely improve performance.
OPTIMIZE TABLE `wp_terms` , `wp_term_taxonomy` , `wp_term_relationships`; |
Good luck 🙂