WordPress Database Optimisation: How to Clean Up and Speed Up Your Database Safely
WordPress database optimisation step by step: back up first, remove revisions, transients and spam, fix heavy autoloaded options, keep the database lean.

On this page
- Signs your database needs attention
- What bloats a WordPress database
- Check the database size first
- Step 1: Back up, and know how to restore
- Step 2: Clean up with a plugin
- Step 3: The WP-CLI way
- Step 4: Find and fix large autoloaded options
- Step 5: Stop the clutter coming back
- WooCommerce stores need extra care
- What "optimise tables" actually does
- How often to clean the database
- When the problem is really the hosting
- WP-Optimize vs Advanced Database Cleaner
- WooCommerce built-in tools
- What transients are, in plain words
- Quick check: how heavy is your database?
- When to get help
- FAQs
- Take a tested backup before any cleanup; database changes cannot be undone with the browser back button.
- Large autoloaded options from old plugins usually slow a site more than revisions or spam, so check the autoload size before deleting anything else.
- Limit revisions, clear expired transients on a schedule and remove leftover plugin tables so the database stays lean without frequent manual cleanups.
WordPress database optimisation means removing data the site no longer needs (old revisions, expired transients, spam comments, leftovers from deleted plugins) and fixing the heavy settings that load on every page, so queries run faster and backups shrink. Done safely, it is a short job: back up, check what is actually large, clean with a plugin or WP-CLI, then put limits in place so the clutter does not return.
The important word is safely. A careless cleanup can delete WooCommerce sessions mid-checkout or remove settings a plugin still needs. This guide shows the order I follow on client sites and what to leave alone.
Signs your database needs attention
- The WordPress admin feels slow even on simple screens such as the dashboard or Posts list.
- Time to first byte is high on uncached pages, while static files load quickly.
- Backups take much longer or have grown far bigger than your content suggests.
- Site Health warns about autoloaded options, or your host flags heavy database usage.
- The site has been running for years with many plugins installed and removed along the way.
If pages are slow but the admin is fast, the cause is more often images, scripts or hosting. Run a proper test first; our guide to website speed test tools explains how to read the results.
What bloats a WordPress database
| Data | Where it lives | Safe to remove? |
|---|---|---|
| Post revisions and auto-drafts | wp_posts, wp_postmeta | Yes, keep a few recent revisions |
| Expired transients | wp_options | Yes, they are temporary cache |
| Spam and trashed comments | wp_comments, wp_commentmeta | Yes |
| Orphaned post meta | wp_postmeta | Usually, after a backup |
| Leftover plugin tables and options | Custom tables, wp_options | Only once you confirm the plugin is gone for good |
| Logs from security, email or redirect plugins | Custom tables | Trim old entries, keep recent ones |
| WooCommerce sessions and action scheduler logs | Custom tables | Clear old completed actions; leave active sessions |
Check the database size first
Do not guess. Open phpMyAdmin from your hosting panel (on Hostinger it is under Databases), select the database and sort tables by size. Look for any table that is far bigger than the rest. Common culprits are wp_options, wp_postmeta, log tables from a security or redirect plugin, and wp_actionscheduler_logs on WooCommerce stores.
Also check the autoload size, explained in step 4. That single number explains more slow WordPress admins than everything else on this page.
Step 1: Back up, and know how to restore
Take a full database backup with your backup plugin or the host's tool, download a copy, and make sure you know how to restore it. A backup you have never restored is a hope, not a plan. Our WordPress backup and restore guide walks through it. On a busy store, do the work at a quiet hour or on a staging site first.
Step 2: Clean up with a plugin
For most site owners a cleanup plugin is the safest route. WP-Optimize and Advanced Database Cleaner are popular; several caching plugins (including LiteSpeed Cache and WP Rocket) also include database tools. Typical safe choices:
- Delete post revisions, keeping the last few per post if the option exists.
- Delete auto-drafts and trashed posts.
- Delete spam and trashed comments.
- Delete expired transients.
- Remove orphaned post meta and comment meta.
Avoid "delete all transients" on a live WooCommerce store during trading hours, and do not tick options you do not understand. Run the cleanup once, check the site front end and admin, then remove the cleanup plugin if you will not schedule it, since an unused plugin is one more thing to update.
Step 3: The WP-CLI way
If your host offers SSH (most managed and VPS plans do), WP-CLI is faster and more precise. Useful commands:
wp db export before-cleanup.sql
wp transient delete --expired
wp post list --post_type=revision --format=count
wp comment delete $(wp comment list --status=spam --format=ids) --force
wp db optimize
Count before you delete, and export first every time. If you are not comfortable with the command line, the plugin route gives the same result for typical sites.
Step 4: Find and fix large autoloaded options
Every page load reads all options marked autoload in wp_options. If that set grows to several megabytes, every request carries the weight, cached or not, for the admin and checkout. Recent WordPress versions show a Site Health warning when the autoloaded total is large.
In phpMyAdmin, sort wp_options rows with autoload set to yes (or on) by the length of option_value. Typical heavy items are settings from plugins you removed years ago, page builder caches, and statistics stored by SEO or security plugins. For each large row:
- Identify the plugin from the option name prefix.
- If the plugin is gone, delete the option (after the backup).
- If the plugin is active, check its settings for a "clear data" or log limit option, or set the option to not autoload if the plugin does not need it on every page.
This is where an experienced developer earns their fee, because deleting the wrong option can reset a plugin's settings. If unsure, ask for help rather than guessing.
Limit revisions rather than deleting them all every week, or editors lose their undo history; WordPress revisions not showing explains the settings involved.
Step 5: Stop the clutter coming back
- Limit revisions by adding define('WP_POST_REVISIONS', 5); to wp-config.php.
- Set log retention in security, email and redirect plugins to 30 to 90 days.
- Uninstall plugins properly; some have a "delete data on uninstall" setting you must tick first.
- Schedule a monthly cleanup of expired transients and spam.
- Use an object cache (Redis or Memcached) if your host supports it, so repeated queries are not run on every request.
A real system cron, explained in our WordPress cron guide, also helps scheduled cleanup and WooCommerce actions run on time instead of piling up.
WooCommerce stores need extra care
Stores grow databases fastest because each order, customer and scheduled action adds rows. Turn on High-Performance Order Storage if your extensions support it, so orders live in dedicated tables rather than wp_posts and wp_postmeta. Clear completed and failed scheduled actions older than a month from WooCommerce, Status, Scheduled Actions. Never delete orders to save space; GST records and customer history depend on them. Our WooCommerce speed optimisation guide covers the rest of the store side.
What "optimise tables" actually does
The optimise button runs OPTIMIZE TABLE, which rebuilds a table to reclaim space left by deleted rows. On modern InnoDB tables the speed gain is usually small; the cleanup before it matters more. It can briefly lock large tables, so run it at a quiet time. If your tables still use the old MyISAM engine, converting them to InnoDB (with a backup and ideally on staging) is often a bigger improvement.
How often to clean the database
For a typical business site, a light cleanup every month and a proper review of autoloaded options and leftover tables once or twice a year is enough. Busy stores and sites with heavy logging may need the light cleanup weekly. Include it in your WordPress maintenance checklist so it happens without anyone remembering.
If your WordPress admin is slow and you want it looked at properly, database work is part of my WordPress speed optimisation service, or it can run monthly under WordPress maintenance.
A bloated database is only one reason pages lag. For the full list of checks, see why a WordPress site is slow and how to find the real bottleneck.
When the problem is really the hosting
If the database is already lean and queries are still slow, the limit may be the server: shared plans with little memory, an old MySQL or MariaDB version, or a busy neighbour on the same server. Check your PHP and database versions in Site Health and compare plans in our guide to shared vs VPS vs managed hosting before spending more time on cleanup.
WP-Optimize vs Advanced Database Cleaner
WP-Optimize combines database cleanup with caching and image compression. Its database tab lists each cleanup type with the number of items found, and you can schedule cleanups. It suits owners who want one tool and a simple interface. If you already use another caching plugin, use only its database features and leave its cache switched off, so two caches do not fight.
Advanced Database Cleaner focuses only on the database. Its strength is showing orphaned tables, options and cron jobs left by removed plugins, which is exactly the clutter other tools miss. Check carefully before deleting anything it marks as orphaned, because detection is based on names and can be wrong for plugins that are still active.
Whichever you use, run it manually the first time and read every item. Once you trust the result, schedule only the safe items (revisions beyond your limit, spam, expired transients).
WooCommerce built-in tools
WooCommerce, Status, Tools has buttons that are safer than generic cleaners for store data: clear expired transients, clear customer sessions (logs out active carts, so use it at a quiet hour), regenerate product lookup tables and delete orphaned variations. Use these first on a store. Never delete orders, customers or order notes; they are business and tax records, and reports, refunds and GST invoices depend on them.
What transients are, in plain words
Transients are temporary cached values plugins store in the database, such as a social feed or an API response, each with an expiry time. Expired ones should be removed automatically, but on sites where WordPress cron does not run reliably they pile up. Deleting expired transients is always safe; deleting active ones is also safe in principle because plugins rebuild them, but it causes a short burst of slower pages while they are recreated.
Quick check: how heavy is your database?
- Check the size: go to Tools, Site Health, Info and open the Database and Directories and Sizes sections to see the total database size.
- Look at autoloaded options: in phpMyAdmin (from your hosting panel), run
SELECT SUM(LENGTH(option_value)) FROM wp_options WHERE autoload IN ('yes','on');. Changewp_to your table prefix. A result well over 1 MB is worth looking into. - Count revisions: in the same SQL tab run
SELECT COUNT(*) FROM wp_posts WHERE post_type='revision';. Thousands of revisions on a small site means cleanup will help.
When to get help
Get help when the database is several hundred megabytes, when a WooCommerce store has years of orders and sessions, or when you are unsure which tables belong to plugins you removed. Always take a backup first. WordPress speed optimisation includes database cleanup with before and after timings, and WordPress maintenance keeps it tidy each month. Get in touch with your database size from Site Health.
Related fixes: WordPress admin bar or dashboard missing.
FAQs
How do I optimise my WordPress database?
Back up first, check which tables and options are largest, then remove revisions, expired transients, spam and orphaned data with a plugin such as WP-Optimize or with WP-CLI. Finally fix heavy autoloaded options and set limits so the clutter does not return.
Is it safe to delete post revisions?
Yes, after a backup. Revisions are old copies of your content. Keeping the last few per post is sensible, and you can limit future revisions in wp-config.php.
Will database optimisation make my site faster?
It helps most when the admin is slow, uncached pages have a high server response time, or autoloaded options are large. If the slowness comes from images, scripts or hosting, database cleanup alone will not fix it.
What are autoloaded options in WordPress?
They are settings in the wp_options table that WordPress loads on every request. When old or bloated plugins store large data there, every page load slows down, so trimming them is often the biggest single gain.
Can I delete tables left by old plugins?
Yes, once you are sure the plugin will not be reinstalled and you have a backup. Check the table prefix matches the removed plugin, and test the site afterwards.
How often should I optimise the WordPress database?
A light cleanup monthly is enough for most business sites, with a deeper review once or twice a year. Busy WooCommerce stores may need weekly cleanup of sessions and scheduled actions.
Need help with your website?
I'm a freelance WordPress developer building fast, SEO-friendly websites since 2020. Tell me what you need and I'll reply with a plan and a fixed quote, usually within 24 hours.


