DISCLAIMER: this post is older than one year and may not be up to date with latest WordPress version.

After an intensive use of your WordPress blog (creating posts, editing posts, and deleting posts) your database will contain thousands of rows of useless meta data. Here is a simple SQL query to delete orphaned post meta on your database.

Just run the following query on your WordPress database to delete orphaned post meta. Don’t forget to replace the table prefix wp_ if your database is using another prefix.
And of course, create a backup of your database before running the query!

FROM wp_postmeta pm
LEFT JOIN wp_posts wp ON wp.ID = pm.post_id

Thanks to Patrick Rauland for submitting this useful recipe!



  1. Elliott richmond

    That is perfect! Thanks so much, I was looking to do this recently on over 2000 posts, something I was going to go back to when I had more time; This just helps me get on with it without having to figure it out myself 😉 thanks again

  2. Use the plugin “Optimize Database”. It will compact your database daily to keep lots of “orphaned” junk from building up in there.

    It also empties your “trash” directories and truncates the mile-long Revisions list for your frequently updated pages.

Leave a Comment

Your email address will not be published. Required fields are marked *