Destructive
MySQL / MariaDB
Clean Orphaned Post Meta
A direct SQL query to identify and remove row entries in wp_postmeta that are linked to post IDs that no longer exist in wp_posts.
// Code goes here What happens to the data?
meta_id
post_id
meta_key
status
1045
102 (Exists)
_edit_lock
Kept
1046
85 (Deleted)
_thumbnail_id
Removed
1047
102 (Exists)
_yoast_seo
Kept
The query joins wp_postmeta with wp_posts. If the post ID is NULL in the join (meaning it's gone), the meta row is deleted.
Execution Methods
$_
WP-CLI (Recommended)
Faster and less likely to timeout on large databases.
wp db query "DELETE pm FROM wp_postmeta pm LEFT JOIN wp_posts p ON pm.post_id = p.ID WHERE p.ID IS NULL"
SQL
phpMyAdmin / TablePlus
Run directly in the "SQL" tab of your database manager.
Paste code from above -> Execute
Did this clean your database?
Help us verify this solution for other developers.