Ever let your WordPress customers database get a bit uncontrolled? If you don’t permit consumer registration however have too many customers that haven’t any posts or cause to be in your site here’s a fast SQL question to clear these customers out of your database. Yes you possibly can in all probability simply delete them from the dashboard, and sure this can be a fairly uncommon use case… however I wanted this simply now so I’m sharing it!

Make a backup of your database earlier than you learn any additional. I am not accountable should you delete your customers and can’t convey them again. You’ve been warned.

First delete the customers, the place the wp_users.ID is just not discovered within the wp_posts.post_author column. We use distinct right here as a result of a single consumer ID might be the writer of one thousand posts, and be returned one thousand occasions once we solely want it as soon as.

delete from wp_users the place wp_users.ID not in (choose distinct(post_author) from wp_posts);  

Think your accomplished? Look in your wp_usermeta desk!

Now we delete all of the wp_usermeta entries the place the wp_usermeta.user_id just isn’t discovered within the wp_users.ID column. Similair to the final question however simply pointing at totally different tables and eradicating the distinct because the ID column is already all distinctive values.

delete from wp_usermeta the place wp_usermeta.user_id not in (choose ID from wp_users);