Safe Database Cleanup, Action Scheduler Hygiene & Automated Schedules
Clean post revisions, auto-drafts, trash, expired transients, orphaned metadata, and completed Action Scheduler logs in bounded cursor batches while protecting WooCommerce orders.
- Cleans 8 targeted bloat categories (
revisions,auto_drafts,trash_posts,trash_comments,spam_comments,transients,actions,orphans) in bounded primary-key cursor batches—never locking InnoDB tables. - Hardcoded eCommerce & Editor Protections (
DatabaseCleanupItems.php): Explicitly excludesshop_order*,shop_subscription,edd_payment,attachment,order_notecomments, open editor post locks (postLocked), and your latest$keeprevisions per post. - Safe Action Scheduler Hygiene: Deletes only aged
complete(> 7d),canceled(> 7d), andfailed(> 30d) rows viaActionScheduler::store()->delete_action()after recheckinglive_claimandrecent_log—never touchingpendingorin-progressjobs! - Includes an Itemized Confirmation Modal, Last Cleanup Run Receipt Table, automated schedules, and
--dry-runWP-CLI inspection.
Why Unchecked WordPress & WooCommerce Databases Slow Down Over Time#
While Early PHP Delivery serves anonymous readers in ~14 ms with 0 SQL queries, your MySQL / MariaDB database still powers every logged-in wp-admin save, WooCommerce /checkout/ transaction, inventory lookup, and cache-miss build. Over 12 to 24 months on an active store, three tables silently balloon into millions of dead rows:
wp_actionscheduler_actions&wp_actionscheduler_logs: WooCommerce Subscriptions, webhooks, marketing syncs, and SEO plugins log tens of thousands ofcomplete,canceled, andfailedbackground tasks every month.wp_posts&wp_postmeta: Saving a 4,000-word landing page 60 times creates60full HTML copies (revisionrows) plus orphaned_elementor_data/ custom-fieldpostmetarows whose parent posts were deleted long ago.wp_optionsExpired Transients: Plugins that set temporary transients without reading them again leave thousands of expired_transient_timeout_*rows sitting insidewp_options.
KadiCache Free Core (DatabaseCleanup.php and DatabaseCleanupItems.php) cleans all eight categories safely inside WP Admin -> KadiCache -> Database without running dangerous DELETE ... JOIN table-locking queries.
Complete Reference: All 8 Cleanup Categories & Built-In Safety Guards#
Every single cleanup category in DatabaseCleanupItems.php enforces strict age, post-type, and ownership rechecks immediately before deletion:
Category Key (--items) | UI Label in Database Tab | Exact Safety Guard Enforced in DatabaseCleanupItems.php |
|---|---|---|
revisions | Post revisions | Keeps the newest database_keep_revisions revisions per parent post; skips trashed parents, active editor autosaves (wp_is_post_autosave + postLocked), and all shop_order* / shop_subscription / edd_payment types; deletes via core wp_delete_post_revision() |
auto_drafts | Auto-drafts | Deletes only auto-draft rows older than 7 days that are not open/locked in an editor and contain zero WooCommerce order_note comments |
trash_posts | Trashed posts | Respects EMPTY_TRASH_DAYS retention window, skips locked posts and order/payment post types, and deletes via wp_delete_post($id, true) |
trash_comments | Trashed comments | Respects trash retention age and explicitly excludes comment_type = 'order_note' so WooCommerce order audit trails are never touched! |
spam_comments | Spam comments | Removes comment_approved = 'spam' (excluding order_note) cleanly via wp_delete_comment($id, true) |
transients | Expired transients | Deletes only expired _transient_timeout_* (and _site_transient_timeout_* when authorized) where timeout < $started; automatically skips DB scan when an external persistent object-cache.php manages transients in RAM! |
actions | Action Scheduler records | Touches only complete (> 7 days), canceled (> 7 days), and failed (> 30 days) rows—rechecking live_claim IS NULL and recent_log = 0 right before calling ActionScheduler::store()->delete_action()! |
orphans | Orphaned metadata & relationships | Removes bounded batches (<= 1,000 postmeta/commentmeta, <= 200 term_relationships) where post_id > 0 and NOT EXISTS reconfirms no parent row was created mid-run |
Bounded Cursor Batching, Confirmation Modal & Post-Run Receipt#
Running a giant DELETE FROM wp_postmeta query on a live store locks MySQL for 45 seconds and causes checkout 504 Gateway Timeout errors. KadiCache prevents that completely:
- 1-Click
Safe Quick CleanPreset: Selects low-risk everyday hygiene categories while keeping your configured revision retention count intact. - Itemized Pre-Run Confirmation Modal: Before a single row is deleted, KadiCache displays exact candidate counts per category and active retention safeguards.
- Primary-Key Cursor Background Batches (
WHERE ID > %d ORDER BY ID LIMIT ...): Cleanup executes asynchronously in bounded background slices with zeroOFFSETscan degradation and zero table locks. - Detailed
Last Cleanup RunReceipt Table: Logs the exact timestamp, status pill, processed batch count, elapsed duration, and proportional row breakdown per category for full auditability.
# 1. Inspect live candidate counts across all 8 categories (0 deletions)
wp kadicache db status --format=table
# 2. Dry-run specific categories first in CI / SSH
wp kadicache db clean --items=revisions,transients,actions --dry-run
# 3. Queue bounded background cleanup for selected categories
wp kadicache db clean --items=revisions,auto_drafts,spam_comments,transients,actionsRecommended Retention Policy for Agencies & WooCommerce Stores#
For active WooCommerce catalogs and multi-author blogs, keeping 3 to 5 recent revisions per post alongside Weekly scheduled cleanup of auto_drafts, spam_comments, transients, and completed actions keeps wp_posts and wp_actionscheduler_logs lean year-round while giving content editors plenty of undo history. On WordPress Multisite installations, DatabaseCleanupItems::canCleanSiteTransients() restricts network wp_sitemeta transient cleanup strictly to the main site under Super Admin, WP-CLI, or cron contexts so subsite admins never touch shared network tables.
Frequently Asked Operational Questions
Can Database Cleanup ever delete a WooCommerce order, subscription, or customer order note?
Will cleaning Action Scheduler records cancel pending WooCommerce subscription renewals or active webhooks?
What happens if an editor is actively writing a draft while scheduled Database Cleanup runs?
Can an AI assistant run database cleanup without my permission via MCP?
How often should I schedule automatic Database Cleanup?
Creator of KadiCache’s Early PHP Drop-In, 7-Driver Object Cache Engine, Cloud Remove Unused CSS, and Zero-CLS Lazy Render pipeline.