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 orders that are not associated with an order.

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);

Let’s Get Started!

Take the stress out of configuring and managing the technical side of your website.

Scroll to Top