Created
August 12, 2023 18:14
-
-
Save nboaldin/2f0f43934b977c2e3f47eb10ca84b3a1 to your computer and use it in GitHub Desktop.
Find and Delete Users Without Orders from WooCommerce
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Find the users first. Added a limit of 1000 to reduce the size of the query. | |
*/ | |
SELECT | |
* | |
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) | |
LIMIT 1 , 1000 | |
/** | |
* Next do a delete if all looks good. Notice it's limited to 100. Increase or decrease to your liking. | |
*/ | |
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) LIMIT 100 | |
/** | |
* Clear User Meta | |
*/ | |
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