WooCommerce Database Cleanup Queries
Read-only diagnostic SQL first, then the matching WP-CLI commands to clean transients, sessions, Action Scheduler logs and more.
Your details
Diagnostic queries and cleanup commands
-- STEP 1: DIAGNOSE (read-only, safe to run any time)
-- Run these in phpMyAdmin, or "wp db query", to see what's actually there before deleting anything.
-- Expired transients
SELECT COUNT(*) AS expired_transients FROM wp_options WHERE option_name LIKE '\_transient\_timeout\_%' AND option_value < UNIX_TIMESTAMP();
-- Expired WooCommerce sessions
SELECT COUNT(*) AS expired_sessions FROM wp_woocommerce_sessions WHERE session_expiry < UNIX_TIMESTAMP();
-- Action Scheduler: complete, failed or canceled actions older than 30 days
SELECT COUNT(*) AS old_actions FROM wp_actionscheduler_actions WHERE status IN ('complete','failed','canceled') AND scheduled_date_gmt < DATE_SUB(NOW(), INTERVAL 30 DAY);
-- Post revisions
SELECT COUNT(*) AS revisions FROM wp_posts WHERE post_type = 'revision';
-- Auto-drafts
SELECT COUNT(*) AS auto_drafts FROM wp_posts WHERE post_status = 'auto-draft';
-- Spam comments
SELECT COUNT(*) AS spam_comments FROM wp_comments WHERE comment_approved = 'spam';
-- Trashed posts and pages
SELECT COUNT(*) AS trashed_posts FROM wp_posts WHERE post_status = 'trash';
-- The 20 largest autoloaded options
SELECT option_name, LENGTH(option_value) AS bytes FROM wp_options WHERE autoload IN ('yes','on') ORDER BY bytes DESC LIMIT 20;
# STEP 2: BACK UP YOUR DATABASE, THEN CLEAN UP
# Run over SSH with WP-CLI installed, from your WordPress root.
wp transient delete --expired
# Runs WooCommerce's own daily session cleanup right now instead of waiting for cron
wp action-scheduler run --hooks=woocommerce_cleanup_sessions
wp action-scheduler clean --batch-size=500 --status=complete,failed,canceled --before="30 days ago"
wp post delete $(wp post list --post_type=revision --format=ids) --force
wp post delete $(wp post list --post_status=auto-draft --format=ids) --force
wp comment delete $(wp comment list --status=spam --format=ids) --force
wp post delete $(wp post list --post_status=trash --format=ids) --force
Take a full database backup before running the Step 2 commands. --force skips WordPress's trash and deletes rows outright.
No SSH or WP-CLI access? Run the Step 1 SELECT queries in phpMyAdmin to see the numbers, then ask your host or developer to run the matching cleanup, or use a plugin such as WP-Optimize or Advanced Database Cleaner.
wp action-scheduler is a built-in WP-CLI command that ships inside the Action Scheduler library WooCommerce already uses, so nothing extra needs installing.
Diagnose before you delete
A WooCommerce database bloats fast: expired transients, old Action Scheduler log rows, orphaned sessions and endless post revisions all pile up in wp_options, wp_posts and their own tables. This tool builds two things: a set of read-only SELECT queries so you can see exactly how big each problem is first, and the matching WP-CLI commands to actually clean it up.
- Transients are WordPress's own cache, expiring on their own; deleting expired ones just reclaims space early.
- WooCommerce sessions (
wp_woocommerce_sessions) hold guest carts and expire on their own timestamp; WooCommerce cleans them up on its own dailywoocommerce_cleanup_sessionsscheduled action, which moved onto Action Scheduler in WooCommerce 10.1. - Action Scheduler (
wp_actionscheduler_actionsand friends) logs every scheduled job WooCommerce and its extensions run. A store with a lot of automation can build up hundreds of thousands of old rows here. - Post revisions, auto-drafts, spam comments and trash are ordinary WordPress content bloat, not WooCommerce-specific.
Running it
- 01
Run the Step 1 queries
In phpMyAdmin, Adminer or
wp db query, to see real counts before deleting anything. - 02
Back up your database
A full backup, or at minimum a snapshot from your host, before Step 2.
- 03
Run the Step 2 WP-CLI commands
Over SSH, from your WordPress root. They use WordPress's and Action Scheduler's own delete logic (via
--forcewhere needed) rather than rawDELETEstatements, so related caches and hooks still fire correctly.
No SSH access? Run the Step 1 diagnostics anyway to know what's actually large, then hand the numbers to your host or developer, or use a cleanup plugin such as WP-Optimize or Advanced Database Cleaner for the same jobs from wp-admin.
Pitfalls
--forcedeletes outright, skipping WordPress's trash step. There's no undo beyond your backup.- A huge Action Scheduler table can take a while to clean in one pass; the generated command batches 500 rows at a time so it doesn't time out.
- If your host has a very short max_execution_time, run the WP-CLI commands in smaller batches, or ask your host to raise it for the session.
Questions people ask
Is this safe to run on a live store?
The Step 1 queries are read-only and always safe. The Step 2 commands change data, so back up first and run them at a quiet time.
Will this speed up my store?
A large autoloaded-options total or a huge Action Scheduler table both add real overhead to every page load, so cleaning them up usually helps. A bloated database rarely causes a slow storefront by itself, though; check the WordPress security check and your hosting plan too.
What is autoload, and why does it matter?
Every option with autoload set to yes is loaded into memory on every single page view, admin or storefront. A single huge autoloaded option (some plugins store big arrays there) can slow down everything on the site.
Do I need Action Scheduler installed separately?
No. It ships bundled inside WooCommerce (and several other plugins), so its WP-CLI commands are already available once WooCommerce is active.
Related tools
Last reviewed 30 September 2026.
Sources: WP-CLI: wp media regenerate · Action Scheduler: WP-CLI commands · WooCommerce Developer Blog: session management and cron changes in WooCommerce 10.1
