SQL delete orphaned post meta data

When using lots of plugins and different themes or when testing your own custom made theme or plugin, a lot of post meta can remain in your database that is not linked to a post. By applying the following query to your database, you can delete all meta data that is no longer linked to a post.

DELETE pm
FROM wp_postmeta pm
LEFT JOIN wp_posts wp ON wp.ID = pm.post_id
WHERE wp.ID IS NULL
Robbert Vermeulen

Need help from a WordPress expert?

I would love to hear about your project and how I can help you achieve your goals