Skip to content

Instantly share code, notes, and snippets.

@taeo
Created February 22, 2019 00:38
Show Gist options
  • Save taeo/82e81801c4f18524272d4a73ade911fa to your computer and use it in GitHub Desktop.
Save taeo/82e81801c4f18524272d4a73ade911fa to your computer and use it in GitHub Desktop.
WooCommerce - delete users with customer role and no orders (likely spam accounts)
/* Delete usermeta that nas no related user in users */
DELETE FROM wp_usermeta WHERE wp_usermeta.user_id NOT IN (SELECT ID FROM wp_users);
/**
* 1. MAKE A BACKUP FIRST!!!!
* 2. DO A PREFLIGHT - USE SELECT * (not DELETE wp_users.*)
*/
DELETE wp_users.* FROM wp_users
INNER JOIN wp_usermeta
ON wp_users.ID = wp_usermeta.user_id
WHERE wp_usermeta.meta_key = 'wp_capabilities'
AND wp_usermeta.meta_value = 'a:1:{s:8:"customer";b:1;}'
AND wp_users.ID not in (
SELECT meta_value FROM wp_postmeta WHERE meta_key = '_customer_user'
) AND wp_users.ID not in (
select distinct(post_author) from wp_posts
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment