Skip to content
Blog

Clean up a bloated WordPress database with WP-CLI

How to clean up a bloated WordPress database with WP-CLI, in a safe order: find the big tables, back up, delete transients and revisions, then check.

The WP Waterfall team · Guides

A WordPress database grows quietly. Revisions, expired transients, and leftover plugin tables pile up until backups take longer and admin pages crawl. This guide shows how to clean up a bloated WordPress database with WP-CLI, in an order that is safe: look, back up, delete, then check.

WP-CLI is the command line for WordPress. On WP Waterfall you do not need SSH to use it. Open the site, pick the environment, choose Tools, then WP-CLI, and type a command without the wp prefix. Each command runs as a job and shows its output and exit code. If a command is refused, the page says why.

The WP-CLI page runs one command on one environment and shows the output when it finishes.

Look before you delete

Start with the size of each table. The Database tab has a Schema view that lists every table with its rows and its size. On most bloated sites the top of the list is one of three things: wp_postmeta, wp_options, or a table a plugin left behind, such as an old log of form entries or security scans.

Then count what is easy to remove:

post list --post_type=revision --format=count
transient list --expired --format=count
option list --autoload=on --format=count

Revisions are old copies of every post edit. Transients are temporary values plugins store with an expiry date, and many plugins never clean up the expired ones. Autoloaded options are loaded on every page view, so a long list slows every request.

Take a backup

Before anything deletes a row, open the Backups tab and choose Back up now. Add a note such as "before database cleanup". A manual snapshot holds the code, the database, and the uploads, and you can restore only the database if you need to. Better still, do the whole cleanup on staging first. Staging is a copy of your site that nobody else sees, and you can reset it from live afterwards.

A manual snapshot with a note sits in the list, ready to restore if the cleanup goes wrong.

Delete what you do not need

Expired transients first. This is the safest command on the list, because WordPress would throw them away anyway:

transient delete --expired

Then revisions. Keep a few if your editors rely on them. List the ids with post list --post_type=revision --format=ids, then pass them to post delete with --force, in batches when the list is long:

post delete 1041 1042 1043 --force

Then plugin leftovers. Deactivating a plugin keeps its tables. The Schema view shows the table, and the plugin's own documentation says whether it is safe to drop. If you are not sure, leave it: a table nobody reads costs disk space and nothing else.

Autoloaded options are the slowest to fix and the most rewarding. List the largest ones:

option list --autoload=on --fields=option_name,size_bytes --orderby=size_bytes --order=desc

A large option from a plugin you removed can go with option delete NAME. One from a plugin you still use should be changed in that plugin's settings, not deleted.

Check the result

Run the counts again, then open the site. Clear all caches on the Caching page so you see real pages. If something is wrong, restore the database from the snapshot you took. If you would rather not type at all, Nami, the assistant in the chat, can delete expired transients for you after you press a confirm button.

For heavy queries that remain after the cleanup, the APM tool lists the slowest tables and statements. See Database access for the Schema view and for running your own SQL.

What to do next

Open the Schema view and find your biggest table. If it belongs to a plugin you removed last year, you have found your afternoon.

Read next

Try what you just read about.

See it in your own dashboard.