Please I just migrated my site and I found out that wp_postmeta consumes most of my cpu resources, Is the any Sql code to Delete all post meta except the featured image. I dont want to Use wp functions for this. Just need only phpmyadmin sql code. I have read this similar Post, But I need the sql code for it, not functions

  • Are you sure you want to do this? There can be a lot of important stuff in meta. Jul 28, 2018 at 3:59
  • Yes, I really want to do this Jul 28, 2018 at 4:02

1 Answer 1


I rewrote the function from the link in your question to SQL.
Keep in mind that this script will also clear the page-templates assigned to the pages.

Make a backup of the wp_postmeta table first.

FROM wp_postmeta pm LEFT JOIN wp_posts p ON pm.post_id = p.id
    (p.post_type IN ('post', 'page') AND pm.meta_key <> '_thumbnail_id')
    OR (p.post_type = 'attachment' AND pm.meta_key <> '_wp_attached_file' 
        AND pm.meta_key <> '_wp_attachment_metadata')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.