Every WordPress site stores its content, settings and plugin data in a MySQL or MariaDB database. Over years of use, it collects clutter: thousands of post revisions, expired temporary data, spam comments and tables left by deleted plugins. A bloated database slows uncached pages, the admin area and backups. Here is how I clean it up safely.
Back up first
Database cleanup deletes data permanently. Before changing anything, take a full backup of the database and files, and confirm you can restore it. Work on a staging site for larger cleanups, especially on WooCommerce stores where orders and customer data are involved.
Note the current database size in your hosting panel or a plugin such as WP-Optimize, and measure page generation time with Query Monitor. These numbers let you see the effect of the cleanup.
Post revisions
WordPress saves a revision every time you update a post or page. Busy sites can accumulate tens of thousands of revisions, which increase database size and can slow some queries. Old revisions are rarely needed after content is final.
Limit future revisions by adding a WP_POST_REVISIONS line to wp-config.php, for example keeping five per post. Then delete old revisions with a cleanup plugin. Keep a few recent revisions so editors can still undo recent changes.
- BackupBefore any change
- RevisionsLimit and clean
- TransientsDelete expired
- AutoloadRemove large leftovers
- Orphan tablesFrom deleted plugins
- Object cacheRedis for dynamic sites
Transients
Transients are temporary cached data stored by WordPress and plugins in the options table, each with an expiry time. Expired transients are supposed to be deleted, but on some sites they accumulate in large numbers.
Cleanup plugins can delete expired transients safely. If you use object caching with Redis or Memcached, transients are stored there instead of the database, which avoids the problem entirely.
Spam, trash and drafts
Spam comments, trashed comments, trashed posts and auto-drafts all take up space. Empty the spam and trash regularly, or let a cleanup plugin do it on a schedule. On sites with comments switched off, old spam can still sit in the database.
WooCommerce sites may have large numbers of old sessions and abandoned cart data. WooCommerce clears expired sessions automatically, but check that scheduled tasks are running.
Autoloaded options
The options table stores site settings, and options marked as autoload are loaded on every page request. When plugins store large amounts of data as autoloaded options, every page has to load it, slowing the site. This is one of the most impactful issues I find.
Query the options table to find the largest autoloaded options, or use a plugin that shows them. Many belong to deleted plugins and can be removed. For options from active plugins, check with the developer before changing autoload settings. WordPress Site Health warns when autoloaded data is very large.
Leftover tables
Deleted plugins often leave their tables behind, sometimes very large ones for logs, statistics or caches. Identify tables that do not belong to WordPress core or active plugins by their prefix and name, then confirm before deleting.
Advanced Database Cleaner and similar tools help identify orphaned tables and which plugin created them. Delete only tables you are sure are unused, after a backup.
Logs and statistics
Security plugins, redirection plugins, email loggers and statistics plugins can store huge logs in the database. Check their settings and limit how long logs are kept, such as 30 or 90 days. Some plugins offer an option to store logs elsewhere or not at all.
Large log tables slow backups and can affect performance. If you use external analytics, consider disabling built-in statistics plugins entirely.
Optimising tables
After deleting data, optimising tables reclaims unused space and can improve performance. Cleanup plugins and phpMyAdmin offer an optimise function. On InnoDB tables, optimisation rebuilds the table, which can take time on large tables.
Run optimisation during quiet periods, since it can briefly lock tables. On very large databases, ask your host for advice, as they may handle maintenance at the server level.
Object caching
Object caching stores database query results in memory with Redis or Memcached, so repeated queries do not hit the database. It is particularly helpful for WooCommerce stores, membership sites and busy admin areas that cannot use page caching.
Many hosts provide Redis with a plugin to connect WordPress. Once enabled, monitor the hit rate to confirm it is working. See WordPress caching explained.
Slow queries and indexes
Query Monitor shows slow database queries and which plugin or theme runs them. Some plugins run inefficient queries on every page. Replacing the plugin or asking the developer for a fix can make a big difference.
Large WooCommerce stores benefit from High-Performance Order Storage, which moves orders into dedicated tables designed for performance. Enable it after checking compatibility with your extensions.
Schedule regular maintenance
Set a cleanup plugin to remove spam, trash, expired transients and old revisions automatically each week or month. Review autoloaded options and leftover tables every few months, especially after removing plugins.
Database care is part of my monthly maintenance checklist and my website protection service. For other speed fixes, see how to speed up a WordPress website.
Key takeaways
- Always back up before cleaning the database.
- Large autoloaded options from old plugins are a common hidden slowdown.
- Limit revisions and log retention to stop bloat returning.
- Use object caching for stores and logged-in users.
Frequently asked questions
Is it safe to delete post revisions?
Yes, after a backup. Keep a few recent revisions for editors.
How big should a WordPress database be?
It varies with content. Size matters less than autoloaded data and slow queries.
Which plugin cleans the database?
WP-Optimize, Advanced Database Cleaner and the cleanup features in WP Rocket or LiteSpeed Cache are common choices.
Will database cleanup speed up my site?
It helps uncached pages, the admin and backups, especially when autoloaded data was large.
What is High-Performance Order Storage?
A WooCommerce feature that stores orders in dedicated tables for better performance on large stores.
How often should I clean the database?
Automated light cleanup monthly, with a deeper review every few months.
