Delete orphan data from post meta in WordPress

What is orphan data?

Sometimes there are entries in postmeta table for those ids which is not exist in main post tables during testing new functionality or developing any plugins.

 

Why you should remove orphan data?

It’s not necessary if you have limited data. But if you have a huge data in your database, then you will need to remove those unnecessary data.

 

Use below code snippet for removing orphan data from postmeta table: TAKE A BACKUP BEFORE RUNNING THE SCRIPT.

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

 

 

 

Leave a Reply