Through the natural course of managing a WordPress site and its database, you may end up with some orphaned user meta entries in the database. “Orphaned” data is a value that belongs to a related ID (in this case, user) that not longer exists in the related table. This is especially true if you run any delete queries on the posts table directly in MySQL, or if you do a lot of plugin/custom code development and/or testing.
The following MySQL queries can help you check for and delete any orphaned user meta entries. These are entries that exist in the wp_usermeta table for which no associated user ID exists in the wp_users table.
IMPORTANT: be sure to backup your database before running any direct MySQL queries – especially those that delete data.
Get a list of orphaned user meta
SELECT * FROM rg_usermeta um LEFT JOIN rg_users u ON u.ID = um.user_id WHERE u.ID IS NULL
Delete orphaned user meta
DELETE um FROM wp_usermeta um LEFT JOIN wp_posts u ON wp.ID = um.user_id WHERE u.ID IS NULL
Leave a Reply