WordPress Development
WordPress Database Optimisation: How to Clean, Speed Up, and Maintain Your Database
Database bloat is the silent performance killer on WordPress sites. Here is the complete maintenance framework for keeping your database lean and fast.
Simple Automation Solutions
··⌛ 9 min read
Every action on your WordPress site writes to the database: publishing posts, saving revisions, storing plugin options, logging spam comments, processing WooCommerce orders. Over time, this accumulates unused data that bloats your database, slows queries, and increases server overhead. Regular database optimisation is one of the most overlooked maintenance tasks.
What accumulates in a WordPress database
| Data type | Source | Grows uncontrolled when |
|---|---|---|
| Post revisions | WordPress autosave and manual saves | No revision limit is set |
| Trashed posts | Posts moved to trash but not permanently deleted | Trash is not emptied regularly |
| Spam comments | Akismet spam queue | Spam queue is never cleared |
| Transients | Plugins storing temporary data | Plugins do not clean up expired transients |
| Orphaned postmeta | Plugin settings attached to deleted posts | Plugins leave behind metadata on post deletion |
| WooCommerce sessions | Guest checkout sessions | Sessions are not purged regularly |
| Unused plugin data | Plugin database tables from deactivated plugins | Plugins do not clean up on deactivation |
A WordPress database that has grown to several hundred megabytes without optimisation can be 3-5x slower on common queries than a well-maintained database of the same content. The slowdown is not linear — as database tables fragment and grow, query execution time increases disproportionately.
Step 1 — Limit post revisions
WordPress saves a revision every time you save or autosave a post. On an active site, a single post can accumulate hundreds of revisions, each stored as a complete copy of the post in the database. Add this to your child theme functions.php or wp-config.php:
Add define('WP_POST_REVISIONS', 5); to wp-config.php to limit all posts to 5 revisions. Alternatively, add add_filter('wp_revisions_to_keep', function($num){ return 5; }); in functions.php for more flexibility. After setting the limit, bulk-delete existing revisions using WP-Optimise.
Step 2 — Clean up with WP-Optimise
WP-Optimise is the most widely used WordPress database cleaning plugin. It targets every category of database bloat in one interface.
Go to Plugins › Add New, search WP-Optimise, install and activate.
Go to WP-Optimise › Database. The plugin analyses your database and shows how much space each type of data is consuming.
Select Clean post revisions. WP-Optimise shows how many revisions exist and how much space they consume. Run the clean. Note: this permanently deletes revision history.
Clean trashed posts, spam comments, expired transients, and orphaned postmeta. Each category shows the count and estimated space savings before you confirm.
After cleaning data, run Optimise Tables. This defragments database tables and reclaims the space freed by deletions — similar to defragmenting a hard drive.
Set WP-Optimise to run weekly or monthly automatically. Consistent maintenance prevents bloat from accumulating between manual cleanups.
Step 3 — Add object caching
Object caching stores the results of expensive database queries in memory (RAM) so subsequent identical queries retrieve the result from memory rather than running the query again. For sites with Redis or Memcached available on their hosting server, enabling object caching can reduce database query load by 40-70%.
- Redis: the most widely supported object cache backend. Available on WP Engine, Kinsta, Cloudways, and most managed WordPress hosts.
- Redis Object Cache plugin: connects WordPress to a Redis server with one configuration step. Go to Settings › Redis after installing to verify the connection.
- Memcached: an alternative to Redis. Supported on fewer WordPress hosts but functionally similar for object caching purposes.
- Check your hosting control panel or contact your host to confirm whether Redis is available on your plan before installing the plugin.
Step 4 — Monitor slow queries
The Query Monitor plugin identifies which database queries on each page are the slowest. Install it and load your homepage, then check the Database Queries panel — queries taking more than 100ms are worth investigating.
- Slow queries are almost always caused by missing database indexes, poorly written plugin queries, or very large tables
- A plugin generating 50+ queries per page is a significant overhead contributor — consider whether a lighter alternative exists
- WooCommerce order tables grow very large on busy stores. Regular archiving of old orders (WooCommerce’s built-in order archiving) keeps the orders table manageable
WooCommerce database maintenance
WooCommerce creates several additional database tables and generates significant data volume on active stores:
- Session cleanup: WooCommerce stores guest checkout sessions. Enable automatic session cleanup in WooCommerce › Status › Tools › Clear customer sessions.
- Order archiving: orders older than a configurable period can be archived rather than deleted, reducing the active orders table size while preserving records.
- Product lookup tables: WooCommerce maintains lookup tables for product attributes and variations. Regenerate these via WooCommerce › Status › Tools if product search performance degrades.
- Log cleanup: WooCommerce logs payment events, webhook deliveries, and API interactions. Clear old logs via WooCommerce › Status › Logs.
Need database optimisation configured for your WordPress site?
Simple Automation Solutions performs database audits, optimisation, and object cache configuration for WordPress sites worldwide.
Frequently asked questions
How often should I optimise my WordPress database?+
Monthly is sufficient for most sites. Sites with very high content publication rates (news sites, active blogs), high WooCommerce order volumes, or many active plugin users may benefit from weekly optimisation. Set WP-Optimise to run on a schedule matching your content publication frequency. A site publishing 5 posts per week accumulates significantly less revision bloat than a site publishing 50.
Can database optimisation break my WordPress site?+
Deleting post revisions and orphaned metadata is safe. Deleting transients is safe — they regenerate automatically when needed. Emptying trash and spam is safe as long as you do not need the deleted content. Optimising database tables (defragmentation) is safe but should be done during low-traffic periods as it briefly locks tables. Always take a database backup before running a major cleanup for the first time.
What is a transient in WordPress?+
A transient is a cached value stored in the WordPress database with an expiry time. Plugins use transients to cache the results of expensive calculations or external API calls — for example, caching the results of a Google Maps geocoding query so the same address is not geocoded on every page load. When a transient expires, WordPress deletes it automatically in theory, but in practice expired transients can accumulate. WP-Optimise and the Delete Expired Transients plugin clean these up reliably.
Simple Automation Solutions is a global digital product studio specialising in WordPress and Bubble.io. We serve founders, startups, and businesses worldwide — delivering production-ready websites built to rank, convert, and scale.
