• I have 13,000 customer (woocommerce) user accounts. I want to delete all accounts who have never placed an order or logged in. I want to delete it directly from the database but I can’t find any queries that can help me.

    What query do I need for user accounts that were created but never logged in?

    Thanks

Viewing 3 replies - 1 through 3 (of 3 total)
  • Try this:

    $users = get_users(['role__in' => ['customer']]);
        foreach( $users as $user ) {
            $args = array(
                'customer_id' => $user->ID,
                'limit' => -1, 
            );
            $orders = wc_get_orders($args);
            if( empty($orders) ) {
                echo "delete user ".$user->ID."\n";
            }
        }

    This client determines all users that have the Customer role and checks if they have an order. If not it goes into the condition to delete. You would have to add the deletion of the user.

    If your customers are not created as customers, you would have to query the respective role as well. Be careful not to include administrators, otherwise you would delete your administrator as well.

    I would recommend running this function via WP CLI as it will take some time.

    And always make a backup before running it.

    Thread Starter lucytech

    (@lucytech)

    thanks.

    could you help me complete the code to delete the user?

    I’m not sure what to put here:

    if( empty($orders) ) { echo “delete user “.$user->ID.”\n”; } // run command to perform the actual deletion.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘delete users who never made an order’ is closed to new replies.