Loading Smiley Hoster
Skip to content

Clean a growing WordPress database without losing useful data

Smiley Hoster Team · · 2 min read
A database cylinder is surrounded by drawers for revisions, transients and autoloaded options.

A growing WordPress database needs an inventory before a cleanup. WP-CLI's option and transient commands help separate automatically loaded settings from temporary data, so you can make a small change and check its effect.

1. Make the starting point recoverable

This workflow assumes a single site with WP-CLI available. Save a private database backup and test restoring it to a staging copy. Record table sizes from your database panel, the date and the site's important functions.

From the WordPress directory, measure autoloaded options:

Example
wp option list --autoload=on --format=total_bytes

List their names and sizes without printing their contents:

Example
wp option list --autoload=on --fields=option_name,size_bytes --format=csv

Sort that output by size in your preferred viewer. A large option is a candidate for investigation, not automatic deletion. Identify the plugin or theme responsible for it.

Keep these results as your baseline. If a command flag is unsupported, check the installed WP-CLI version and local help rather than substituting an improvised SQL deletion.

2. Work through one data category at a time

On staging, begin with expired transients:

Example
wp transient delete --expired

Record the reported result. Avoid replacing the expiry filter with --all out of habit, since that also removes still-valid cached entries. An external object cache can mean the database does not contain the full cache inventory.

Next, decide which posts can lose their older revision history. In a cleanup tool you already use, select revisions only and review its preview. Keep drafts, orders and other business records out of the operation. If the tool cannot show a clear scope, postpone that deletion.

To limit subsequent accumulation, a sample editorial policy in wp-config.php, before the end of configuration, is:

Example
define( 'WP_POST_REVISIONS', 8 );

Eight is an example retention choice, not a universal optimum. Changing this setting is not an immediate cleanup of all existing history.

For autoloaded options, follow the owning component's documented cleanup procedure. Do not erase an option merely because its name looks obsolete, and do not disable autoload globally.

3. Validate behaviour before repeating live

After each category, open the editor, save a disposable draft and test the affected functions: search, forms, customer accounts or the cart. Compare table sizes and the autoload measurement with your baseline.

Physical storage may not shrink immediately after rows are deleted. Do not automatically optimise every table just to make the displayed size smaller.

Repeat only the staging operations that passed these checks on production, during a quiet period and after taking a fresh backup. Then run the same functional checks again.

Common failures come from deleting options by a broad name prefix, cleaning several categories together, or scheduling recurring cleanup before understanding its effect. Keep an operation record that identifies what was removed and which backup can recover it.

Sources: WP-CLI options, WP-CLI transients, WordPress revisions.

Slide 1 of 5: WordPress database care. Clean your database without losing data. Measure first, clean in small batches, and verify every change.
Slide 2 of 5: Step 1 of 3. Back up and benchmark. Back up the database, test the restore on staging, and record table sizes plus key site functions. Investigate large autoloaded options before deleting anything.
Slide 3 of 5: Step 2 of 3. Clean separately. Delete expired transients on staging. Review revisions on their own, keep business records out, and follow documented cleanup for autoloaded options.
Slide 4 of 5: Step 3 of 3. Test every change. Test editing and affected features after each change. Repeat only the staging operations that passed, during a quiet period and after a fresh production backup.
Slide 5 of 5: Clean with confidence. Keep every cleanup measured and reversible. Use the full workflow to establish a baseline, limit the scope, and verify the result.
On social media

See it in our social posts

The key steps of this article, as a carousel. Follow us to catch the next ones.

Did this article answer your question?
Your feedback shapes what we write next.

Your site online today

Free migration* · 30-day refund

Get started* A site under 30 GB, cPanel, WordPress and VPS plans.