20 thoughts on “Delete Orphaned Post Meta Data in WordPress

  1. Thank you so much. This is great tip ๐Ÿ™‚

  2. A year later and I am saying thanks!

  3. Thank you as well for pushing this out there. I too needed to clean up post_meta after dropping 1.2M rows of pseudo-garbage from wp_posts.

  4. Thanks for this code, it worked great!

  5. Hey, is there any danger in running this code?

    • Any SQL query is risky. You could easily mess up some data in your database. Before running any SQL query read through it to make sure it’s doing what you want it to do. In this case it’s pretty straight forward. If you want to be extra cautious create a DB backup before running the query.

      I hope that helps! ๐Ÿ™‚

  6. I am wondering if you have an idea how to remove all the _postmeta data from WordPress/Woocommerce pertaining to FORMER user’s orders. I am going to be exporting my sql db file to work with another person and so I deleted all users and all orders from Woocommerce, incl. from the Trash. However, to make sure I looked through the database file I exported after the deletions and I noticed that much, if not all, the user shipping, billing, etc. data is STILL in the sql file in the _postmeta table with the fields

    (meta_id, post_id, meta_key, meta_value)

    that I ‘thought I deleted’ on the front end. I will see if I can pull some sql tricks but wondered if you had an idea already. Very odd (poor design) this stuff is not removed upon the front end deletion.

    Thanks in advance!

  7. Where Should I put the code? and how does it know which things are suppose to be there? Thanks.

    • This is a SQL statement. You’ll need to open up your database (usually via PHP MyAdmin or some sort of admin interface) and then run a SQL statement, sometimes called a SQL command.

  8. is this code woocommerce “safe” ?

  9. Thanks buddy it helped me to remove about 1000 orphans users!

  10. Want to know were to put this line of code. Someone can help please?

  11. I run this code but the resul was “zero deleted”. But I know there are data entries belonging to dead plugins. What if I delete them individually once I spotted they have that old plugin’s prefix? Is it safe?

    • If you don’t plan on using that plugin anymore there’s no reason why you can’t delete the meta data manually.

      While I like to keep things clean removing meta data manually won’t give you any significant performance boosts. This is helpful when you have thousands of pieces of meta data that can be deleted.

  12. After use sql query my site got 500 error then i am back to backup db where i made miss take? Also i have 19758 orphaned postmeta in my database :/

Leave a Reply

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