How to Delete WooCommerce Customers Who Have Never Made a Purchase

How to Delete WooCommerce Customers Who Have Never Made a Purchase

As your WooCommerce store grows, you may accumulate a significant number of registered users who have never made a purchase. These inactive accounts can clutter your customer database, making it harder to manage and potentially slowing down your site. In this blog post, we’ll explore why you might want to delete these inactive accounts and provide a simple PHP code snippet to help you do so safely.


Why Delete Inactive WooCommerce Customers?

  1. Database Performance: A large number of inactive users can bloat your database, leading to slower query performance and longer backup times. Deleting these accounts can help optimize your database and improve overall site performance.
  2. Data Accuracy: Keeping your customer data clean and relevant is essential for effective marketing and customer relationship management. Removing inactive users helps you focus on customers who are actively engaged with your store.
  3. Security Concerns: Every user account represents a potential security risk. By minimizing the number of accounts in your database, you reduce the attack surface available to hackers.
  4. Cost Management: If you’re using CRM or email marketing tools that charge based on the number of contacts, deleting inactive users can help reduce unnecessary costs.

How to Identify and Delete Inactive WooCommerce Customers

Before proceeding with deletion, it’s important to back up your database. Once you have a backup, you can safely use the following code snippet to identify and delete users who have registered but never made a purchase.

Step 1: Add the PHP Code Snippet

Here’s a PHP code snippet that you can use to delete WooCommerce customers who have never made a purchase:

function delete_customers_without_purchases() {
    global $wpdb;

    // Get all users with the customer role who have never made a purchase
    $customers = $wpdb->get_results("
        SELECT u.ID, u.user_login, u.user_email
        FROM {$wpdb->prefix}users u
        LEFT JOIN {$wpdb->prefix}usermeta um ON u.ID = um.user_id
        LEFT JOIN {$wpdb->prefix}wc_order_stats os ON u.ID = os.customer_id
        WHERE um.meta_key = '{$wpdb->prefix}capabilities'
        AND um.meta_value LIKE '%customer%'
        AND os.customer_id IS NULL
    ");

    if (!empty($customers)) {
        echo '<h2>Deleting Customers Who Have Never Made a Purchase</h2>';
        echo '<ul>';

        // Loop through each customer and delete their account
        foreach ($customers as $customer) {
            wp_delete_user($customer->ID);
            echo '<li>Deleted user: ' . esc_html($customer->user_login) . ' (' . esc_html($customer->user_email) . ')</li>';
        }

        echo '</ul>';
    } else {
        echo '<p>No customers found who have never made a purchase.</p>';
    }
}

// Usage example: Call this function to delete customers
add_action('admin_init', 'delete_customers_without_purchases');

Step 2: Implement the Code

  1. Using a Custom Plugin: You can create a small custom plugin to hold this code. To do this, create a folder named delete-inactive-customers in the wp-content/plugins/ directory. Inside that folder, create a file named delete-inactive-customers.php and paste the code snippet above into it. Then, activate the plugin from your WordPress dashboard.
  2. Adding to Theme’s functions.php: Alternatively, you can paste the code into your theme’s functions.php file. However, using a custom plugin is preferable as it won’t be affected when you update your theme.

Step 3: Execute the Code

The code is designed to run automatically when you access the WordPress admin area. It will scan for WooCommerce customers who have never made a purchase and delete their accounts.

You can also remove the add_action('admin_init', 'delete_customers_without_purchases'); line and call the function manually if you prefer to run it only on demand.

Step 4: Verify Deletion

After running the code, you should verify that the inactive users have been deleted. You can check your user list in WooCommerce or by reviewing your database directly.

Step 5: Disable the Code

Once the code has executed and the inactive users have been deleted, it’s important to disable or remove the code to prevent accidental deletion in the future. If you added the code to a custom plugin, simply deactivate the plugin. If you placed it in your theme’s functions.php, you can comment it out or delete it.


Conclusion

Cleaning up your WooCommerce customer database by removing users who have never made a purchase is a practical step towards optimizing your store’s performance, enhancing security, and maintaining accurate data. The code snippet provided in this post offers a straightforward way to identify and delete these inactive accounts.

Before implementing any deletion process, always ensure you have a recent backup of your database. This allows you to restore any data if needed. Once the inactive users are removed, you’ll have a more streamlined and manageable WooCommerce store.

Feel free to share this post with other WooCommerce store owners, and if you have any questions or need further customization, leave a comment below!

Photo by Markus Spiske on Unsplash

Get A No Obligation Quote

Do You Need Help With Your WooCommerce Site?

Click through to the next page and complete the form to get a free no obligation quote to fix any issue you are having with your WooCommerce site.