Truemag

  • Categories
    • Tips And Tricks
    • Internet
    • PHP
    • Javascript
    • CSharp
    • SQL Server
    • Linux
  • Lastest Videos
  • Our Demos
  • About
  • Contact
  • Home
  • Write With Us
  • Job Request
Home Tips And Tricks WordPress Delete Unused Post Tags By SQL Command

WordPress Delete Unused Post Tags By SQL Command

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 :)

Jun 29, 2011 Hoan Huynh
Simple PHP Code Send EmailLinux Chmod Change Mode Directory Recursive
You Might Also Like:
  • How To Get WordPress Tags By Categories
  • Delete Bulk Draft And Trash WordPress Posts Includes All Tags, Comments, Meta Fields And Terms Associated
  • Delete WordPress Revision Posts, Its Relationships And Meta Data
  • Function Remove All HTML Tags In PHP
  • Auto Delete Old IIS Logs, FTP Logs, SMTP Logs In Windows
  • PHP Delete File Function
  • How To Delete Application In Facebook Developers
  • WordPress Check If Post Is In Category
  • Keep Posting When You’re On Vacation With WordPress Post TimeStamp
  • Delete, Shrink, Eliminate Transaction Log .LDF File
  • http://www.google.com/ Trevion

    Well done aitrcle that. I’ll make sure to use it wisely.

  • http://suckup.de/ Voku

    thx for sharing this SQL-Code :)

  • Carol

    How would you change the command to delete a tag that has less than 3 posts attached to it? 

    • http://4rapiddev.com/ Hoan Huynh

      Hi Carol,

      Simply try to adjust the first SQL statement as below:

      ——————————

      DELETE FROM wp_terms WHERE term_id IN (SELECT term_id FROM wp_term_taxonomy WHERE COUNT < 3 );
      ——————————

      with '3' is the post limit.Please back up your database before trying :)

      Hope this helps.

      H2.

  • T. J. Brumfield

    I inherited a site from someone else with a lot of old content. Some of the old writers came over to the new site, and others didn’t. I removed old posts via SQL for the authors who didn’t come over to the new site, which proved to be a bit of a mistake. Since I didn’t delete the posts manually through WordPress, it didn’t update the tag count. Now I have 3,000 tags, many of which say they have been used when they aren’t being used by actual content on my site.

    Is there a way via SQL I can force WordPress to recalculate actual tag count?

  • dejudicibus

    Does not work in MySQL

Hoan Huynh

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at hoan@4rapiddev.com

8 years ago Tips And Tricksoptimize, performance, table_prefix, Wordpress, wp_term_relationships, wp_term_taxonomy, wp_terms390
0
GooglePlus
0
Facebook
0
Twitter
0
Digg
0
Delicious
0
Stumbleupon
0
Linkedin
0
Pinterest
Most Viewed
PHP Download Image Or File From URL
18,697 views
Notepad Plus Plus Compare Plugin
How To Install Compare Text Plugin In Notepad Plus Plus
14,446 views
Microsoft SQL Server 2008 Attach Remove Log
Delete, Shrink, Eliminate Transaction Log .LDF File
13,326 views
JQuery Allow only numeric characters or only alphabet characters in textbox
10,871 views
C# Read Json From URL And Parse/Deserialize Json
7,461 views
4 Rapid Development is a central page that is targeted at newbie and professional programmers, database administrators, system admin, web masters and bloggers.
Recent Posts
  • Magento Fatal error: Unsupported operand types
  • Ionic bower the name contains uppercase letters
  • PHP Magento Get Manufacturer Name And ID From Magento Product ID
  • Know Simple Hacks To Systematize Media Library in WordPress
  • A handy guideline on adding custom menu item in WordPress admin
Categories
  • CSharp (45)
  • Facebook Graph API (19)
  • Google API (7)
  • Internet (87)
  • iPhone XCode (8)
  • Javascript (35)
  • Linux (26)
  • MySQL (16)
  • PHP (84)
  • Problem Issue Error (29)
  • Resources (32)
  • SQL Server (25)
  • Timeline (5)
  • Tips And Tricks (140)
EMAIL SUBSCRIPTION

Sign up for our newsletter to receive the latest news and event postings.

Recommended
  • CDN
  • Hosting
  • Premium Themes
  • VPS
2014 © 4 Rapid Development