Find and Delete Users Without Orders from WooCommerce | Reggio Digital: WordPress Managed Hosting and Maintenance

Find and Delete Users Without Orders from WooCommerce

Do you have a bunch of users that have made no orders on WooCommerce? Or perhaps they are spam users? Here’s a SQL command that you can run to delete all users that are not associated with an order. If you’re not familiar with running SQL commands on your server, you’ll want to reach out to your hosting provider. You can also run this command within phpMyAdmin if you have access.

Create a backup before running this!

/**
 * 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);
Scroll to Top