The tested SQL queries in this post enable you to delete WordPress users with the customer role who have no orders.
WARNING : Before you start SQL queries, create a backup of your database.
Before you delete the users, run a SELECT query first.

You can check the number of users against the number in your WordPress Dashboard > Users > Customers.

NOTE : This custom database query assumes your table prefix is wp_ You can check this in your wp-config.php file.
SELECT u.ID, u.user_login
FROM wp_users u
JOIN wp_usermeta um ON u.ID = um.user_id
LEFT JOIN wp_posts p ON u.ID = p.post_author AND p.post_type = 'shop_order'
WHERE um.meta_key = 'wp_capabilities'
AND um.meta_value LIKE '%customer%'
AND p.ID IS NULL;
Once checked, you can perform the DELETE query.
DELETE u, um
FROM wp_users u
JOIN wp_usermeta um ON u.ID = um.user_id
LEFT JOIN wp_posts p ON u.ID = p.post_author AND p.post_type = 'shop_order'
WHERE um.meta_key = 'wp_capabilities'
AND um.meta_value LIKE '%customer%'
AND p.ID IS NULL;
Reason For Deletion
In this case, the customers were spam customer registrations which would have been prevented if a captcha was added to the WooCommerce Registration Form and Lost Password Form.
Spam registrations are malicious because you can get blacklisted by Google when thousands of emails are sent out to people who never registered and they mark them as spam.
You might also consider adding the captcha to your WordPress Registration Form and WooCommerce Checkout Form.
Leave a Reply