Cleaning Up Old Post Metadata in WordPress

Over time, as posts are updated, created, or deleted, the wp_postmeta table can become large and inhibit your website performance. In this article, we are going to show you how to clean up old, unnecessary post metadata from the wp_postmeta table to help your WordPress site run more efficiently.

  1. Before continuing, make a backup of your database. This step is critical as we are making mass deletions that can potentially break things on your site.
  2. Log into cPanel.
  3. phpMyAdmin icon

    Next, scroll down to the Databases section and click on phpMyAdmin.

  4. select your database on the left

    Within PHPMyAdmin, select your WordPress database on the left side column. If you are unsure of the database that your WordPress site is using, follow our guide on finding your WordPress database name.

  5. click SQL

    At the top of the page, click on SQL.

  6. new code entered

    Inside the text area, enter the following:

    SELECT * FROM wp_postmeta pm LEFT JOIN wp_posts wp ON wp.ID = pm.post_id WHERE wp.ID IS NULL;
    DELETE pm FROM wp_postmeta pm LEFT JOIN wp_posts wp ON wp.ID = pm.post_id WHERE wp.ID IS NULL;

  7. Then, press the Go button on the bottom right.

PHPMyAdmin will either let you know how many items it removed or state that the result is empty which means that nothing was found. Congratulations! Your wp_postmeta table has now been cleaned up.

7 thoughts on “Cleaning Up Old Post Metadata in WordPress

  1. This is a life saver, but is there a way to automate this clean up on a daily basis? My orphaned meta data seems to be growing by the 10ks every day! Using Divi optimized by WPMUDev Hummingbird Pro.

    1. Good question, Doug. With a situation like this that comes up frequently, there is usually a plugin designed to solve the issue. I tried to find out what was popular, and it looks like the Advanced Database Cleaner plugin may offer this as a feature. Please note, this is not an official recommendation of that particular plugin, and all the usual disclaimers apply: other plugins may to what you are looking for more efficiently, not every plugin is optimized to work with every site configuration, and please be sure to test any plugins before deploying them on your site. I’m just offering the suggestion to point you in the right direction — hope it helps!

    1. It may mean that any plugins you’ve uninstalled didn’t leave behind excess data. That’s a good thing.

  2. Hello,

    thanks for the useful tutorial. I run the queries you suggested and found nothing wrong, but my postmeta data are made huge by the presence of the meta keys related to the views count (daily, weekly, monthly). Any suggestions about these?

    Thanks

Was this article helpful? Join the conversation!