Skip to content

Instantly share code, notes, and snippets.

@mikejolley
Created April 26, 2016 09:37
Show Gist options
  • Save mikejolley/34a32fbda7ccb8029a19ab1e1d2b1dbc to your computer and use it in GitHub Desktop.
Save mikejolley/34a32fbda7ccb8029a19ab1e1d2b1dbc to your computer and use it in GitHub Desktop.
WooCommerce - SQL to Delete users with no orders or posts
DELETE from wp_users where 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
);
delete from wp_usermeta where wp_usermeta.user_id not in (select ID from wp_users);
@greguly
Copy link

greguly commented Dec 2, 2024

For multisites (replace XX with site id):

delete u from wp_users u join wp_usermeta um on u.ID = um.user_id
left join wp_XX_posts p on u.ID = p.post_author and p.post_type = 'shop_order'
where um.meta_key = 'wp_XX_capabilities' and um.meta_value LIKE '%customer%' and p.ID is null ;

delete from wp_usermeta where wp_usermeta.user_id not in (select ID from wp_users);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment