Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

Delete all post meta except featured image Using SQL

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.

DELETE pm 
FROM wp_postmeta pm LEFT JOIN wp_posts p ON pm.post_id = p.id
WHERE
    (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')

Related Posts:

  1. Delete all post meta except featured image
  2. Migrating non-WordPress CMS to WordPress, lots of data to move — possible solutions?
  3. How to solve slow WordPress site caused by attachment_metadata
  4. Set Display Name to first and last name (phpmyadmin SQL Query)
  5. Size article thumbnails and retrieve them directly from database
  6. Emojis getting converted to “?”
  7. WordPress and MySQL: how to transfer Meta_key and Meta_Value from one post_id to another
  8. WordPress database import: Row size too large
  9. Changing root password in PHPMyAdmin for WordPress Database when going live
  10. Can’t update WordFence Options, clear data manually
  11. Files on Localhost, Database on Server
  12. Installing local to live WP into subdirectory
  13. DB prefix not updating
  14. I wanted to Know that how can I give ingore to 0000-00-00 in my config file
  15. I imported an restore an database and It shows #1067 – Invalid default value for ‘user_registered’
  16. “BS_” rows in postmeta table
  17. Migrating meta_key and meta_value from old theme to new one
  18. mySQL database size large, but tables size less
  19. Missing latest posts and options after DB migration via phpMyAdmin
  20. Check if a database is well installed
  21. How do i get value from wp_postmeta?
  22. featured image not showing up with mysql_connect
  23. SQL query : Select a thumbnail for a media
  24. Deleting WordPress posts and media files based on author via MYSQL database
  25. Rename a table in MySQL
  26. How should I tackle –secure-file-priv in MySQL?
  27. Can’t connect to MySQL server on ‘127.0.0.1’ (10061) (2003)
  28. importing a CSV into phpmyadmin
  29. Checking if Database Table exists
  30. The MySQL alternatives: Do Percona Server and MariaDB work well with WordPress, and do they make WordPress go better?
  31. How to fetch Data in WordPress using MySQLi or $wpdb
  32. Connect to database using wordpress wp-config file
  33. How To Export/Import WordPress (MySQL) Database Properly Via Command-Line?
  34. How to define composite keys with dbDelta()
  35. Is it possible to define two databases for one installation?
  36. Dealing with Many Meta Values, 30+
  37. What is the most secure way to store post meta data in WP?
  38. importing data from non-wordpress mysql db
  39. Huge wp_options table
  40. “MySQL server has gone away” since update to 3.8
  41. Error establishing a database connection – with Debug Data
  42. How to execute mulitple statement SQL queries using $wpdb->query?
  43. Does thumbnail regeneration edit the database?
  44. Can I transfer a mysql database to another site?
  45. Database with mixed collation (utf8mb4 & utf8_general_ci)
  46. MySQL Syntax Error upon restoring database from backup [closed]
  47. Help running a MySQL query to update all wp_#_options tables in a Multisite install
  48. Is it safe to convert tables from MyISAM to InnoDB?
  49. How to delete posts that does not have a thumbnail?
  50. Migrating database / content of non-CMS site to WordPress
  51. How do I properly update the WordPress database password?
  52. wpdb_prepare with multiple or condition
  53. Accidentally deleted active_plugins portion of the wp_options DB table
  54. WordPress Install and Database on separate hosting?
  55. delete post meta from db, even if does not exist?
  56. Where does the actual data stored by add_post_meta
  57. Error establishing database connection – high traffic?
  58. Using GROUP CONCAT in my-sql query with wp_usermeta table
  59. Routine to convert custom post meta from old to new value
  60. Create table from array with prepare
  61. How to prevent/delay MySQL connections when an object cache is used?
  62. Uknown meta entries in wp_postmeta
  63. Does post_meta data need to be unserialzed?
  64. Error establishing a database connection,
  65. Changing between Local Host DB to Production DB and in reverse
  66. Post Views / Hit Counter Problem?
  67. How and where is wordpress adding mySQL content to database?
  68. Query Column of Specific ID from Database Table
  69. How to create more than one new wpdb object?
  70. Link image with data from the wpdb
  71. SELECT SQL_CALC_FOUND_ROWS with wordpress search
  72. How Do I Delete WordPress Posts Older Than 400 Days, From A WordPress Category
  73. $wpdb->get_var – What value is returned when zero records are found?
  74. Remote database -> massive response time increase?
  75. Custom Post Type not showing anything added by code anymore?
  76. Getting “Error Establishing a Database Connection” on localhost in new Mac OSX Lion environment
  77. Cloning blog to localhost via WAMP
  78. Delete user with only subscriber role
  79. Can’t log into wordpress site – I have made a new user and still cant access
  80. Counting post and changing roles in mysql database. Query not code
  81. How to save EXIF metadata in WordPress database?
  82. How to edit custom table data in frontend
  83. local wordpress broke after changing URL
  84. Exporting Post ID, Post Title, Primary Category and Primary Category ID
  85. WP website showing blank page after moving from subdomain to main domain
  86. How to create index (sql) to a meta_key?
  87. Database structure for thousands of posts
  88. WPCLI search and replace in a particlar site dir effect another site-dir
  89. After renaming MySQL database, I can no longer access my Blog page
  90. Hang Up Followed By Can’t select database
  91. WordPress character set issue
  92. Create a new table on the database and save parameters from a contact form
  93. Changed meta_key value in structures, now dashboard shows zero users
  94. Why is converting my database to UTF-8 truncating entries?
  95. Database question while Migrating from one domain to another on same WebHost [Bluehost]
  96. How to back-up a database on IIS
  97. Export posts with postmeta without ID?
  98. export individual posts to text files or a single csv file
  99. WordPress database connection failed, while mysql client is working [closed]
  100. SQL errors when querying for something with apostrophes
Categories Database Tags database, mysql, phpmyadmin, post-meta, post-thumbnails
Checking if a post with certain meta value exists
WP_Query no posts with tax_query

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress