Over time, WooCommerce stores can accumulate a lot of unnecessary data—especially from spammy or fake user registrations. These users often have no orders and can clog up your database, slowing things down and making customer management messy.
This SQL snippet will help you identify and remove those unwanted users safely.
⚠️ Important: Always back up your database before running SQL queries!
🧩 The Code
/**
* Step 1: Find users without orders (LIMIT to 1000 results for safety)
*/
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;
/**
* Step 2: Delete users without orders (LIMIT to 100 for controlled cleanup)
*/
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;
/**
* Step 3: Clean up user meta for deleted users
*/
DELETE FROM wp_usermeta
WHERE
wp_usermeta.user_id NOT IN (
SELECT ID
FROM wp_users
);
📝 How It Works
- Find the users without orders (Step 1)
- This query checks for users who:
- Have no orders ( _customer_user meta key not found)
- Haven’t authored any posts (such as blog posts or pages)
- Limits results to 1–1000 for safe inspection.
- Delete users (Step 2)
- Once you’ve confirmed the results look good, run the delete query.
- The limit is set to 100 users to prevent server overload. You can increase or decrease as needed.
- Clear associated user meta (Step 3)
- Cleans up any leftover user meta tied to users who no longer exist.
⚙️ Where to Run This
You can run these SQL queries using:
- phpMyAdmin
- Adminer
- A database management tool like WP-CLI or through your hosting provider’s database panel.
Reminder: Always back up your database first. Once users are deleted, it’s irreversible without a backup!
🚀 Why Use This?
- Removes spammy/fake users with no orders
- Reduces database bloat and improves performance
- Helps maintain a clean and organized user list
🛠️ Need Help Cleaning Up Your Store?
At EUX Digital Agency, we specialize in WooCommerce performance optimization, security, and custom development. If you need help cleaning up your store or preventing spam registrations in the future, we’ve got you covered.
📩 Get in touch with us for tailored support or custom solutions.