During a maintenance of a blog, I needed to clean up the user metadata table since many users was deleted from the main user table (wp_users) with a direct query.
The quick and dirty solution is to run a “left join delete”. Here the SQL statement:
DELETE wp_usermeta FROM wp_usermeta LEFT JOIN wp_users ON wp_usermeta.user_id=wp_users.id WHERE wp_users.id IS NULL
Easy and really quick (please avoid to clean up with a delete plus a sub-query which is the slowest method in the world – other than do it by hand row by row).