← Back to All Snippets

Delete Orphaned Post Meta Records via Direct SQL

⚡ Target: phpMyAdmin / WP-CLI db query 🔒 Tested: MySQL 8.0+ / MariaDB 10.4+ 💻 Lang: SQL
⚠️ Database Table Prefix Reminder: This query uses the standard wp_ table prefix (e.g. wp_options, wp_postmeta). If your installation uses a custom table prefix (configured in wp-config.php, e.g. wp_a7b_), be sure to replace wp_ with your custom prefix before running this query.
SQL (phpMyAdmin / WP-CLI db query)
DELETE pm FROM wp_postmeta pm 
LEFT JOIN wp_posts wp ON wp.ID = pm.post_id 
WHERE wp.ID IS NULL;

When posts or custom post types are deleted without proper cleanup hooks, their metadata remains in wp_postmeta. This query cleans orphaned post meta.

← Back to All Snippets Explore Dev Tools →