WordPress Database Clean: Boost Performance
In this article
This guide shows you how to clean and optimize your WordPress database to improve site speed and responsiveness. You’ll learn to remove unnecessary data like post revisions, spam comments, and transient options, and then optimize the tables. This process is safe and can be done with a plugin or manually via phpMyAdmin, depending on your comfort level.
What You'll Need
- WordPress admin access (to install a plugin, if using that method).
- cPanel or hosting file manager access (for manual method via phpMyAdmin).
- A recent backup of your database (critical – never skip this).
Step-by-Step
- Back up your database. Go to your hosting control panel (like cPanel) and open phpMyAdmin. Select your WordPress database, click “Export” (Quick method, SQL format), and download the file. If you’re using a plugin like UpdraftPlus, create a full backup including the database. Store the backup offsite.
- Install a database optimization plugin (recommended for most users). In your WordPress admin, go to Plugins → Add New. Search for “WP-Optimize” or “Advanced Database Cleaner.” Install and activate one. These plugins handle cleaning and optimization safely.
- Run the plugin’s cleanup. Open the plugin’s settings page (usually under “WP-Optimize” or “Tools → Database”). Select what to clean: post revisions, auto-drafts, trashed posts, spam and trashed comments, transient options, and pingbacks. Keep “Keep last X revisions” set to a low number (like 2 or 5) to retain some history. Click “Run cleanup” or “Optimize.”
- Optimize the database tables. In the same plugin, find the “Optimize tables” button (often on the same screen or a separate tab). This runs the SQL
OPTIMIZE TABLEcommand on each table, which defragments the data and reclaims space. Click it and wait for the process to finish. - If you prefer manual cleanup via phpMyAdmin: Log into phpMyAdmin, select your database. Go to the “SQL” tab. Run the following query to delete all post revisions except the latest one for each post (adjust the number 5 to keep more or fewer):
DELETE a FROM wp_posts a WHERE a.post_type = 'revision' AND a.ID NOT IN (SELECT keep_id FROM (SELECT ID as keep_id FROM wp_posts WHERE post_type = 'revision' ORDER BY post_date DESC LIMIT 5) AS keep_rows);Be careful: this query can be slow on large databases. Alternatively, use the plugin method to avoid raw SQL. - Clean up transients. Transients are temporary cached data. Expired ones are safe to delete. In phpMyAdmin, run:
DELETE FROM wp_options WHERE option_name LIKE '_transient_%' AND option_name NOT LIKE '_transient_timeout_%';Or use the plugin’s “Clean transients” option. - Remove spam and trashed comments. In phpMyAdmin, run:
DELETE FROM wp_comments WHERE comment_approved = 'spam' OR comment_approved = 'trash';Also delete comment meta for those comments:DELETE FROM wp_commentmeta WHERE comment_id NOT IN (SELECT comment_id FROM wp_comments); - Optimize all tables after cleaning. In phpMyAdmin, select all tables (checkbox at bottom), then from the “With selected” dropdown choose “Optimize table.” This runs the same OPTIMIZE command as plugins do.
- Clear any caching plugins. If you use a caching plugin like W3 Total Cache or WP Super Cache, purge the cache after cleaning so the new database state is reflected.
- Verify your site. Visit your homepage and a few posts to ensure everything loads correctly. Check that no images or content are missing.
Common Mistakes
- Skipping the backup. The most common and dangerous mistake. If something goes wrong, you lose data permanently.
- Deleting all revisions. Keeping zero revisions removes the ability to restore a previous version of a post. Keep at least 2-5.
- Deleting active transients. Some transients are used by plugins for caching. Deleting all transients (including non-expired ones) can temporarily slow down your site until they are regenerated. Stick to deleting expired ones or use a plugin that handles this correctly.
- Running the optimization on a live site during peak hours. Optimizing large tables can lock them and cause downtime. Do it during low traffic.
- Using raw SQL without knowing your table prefix. The default prefix is
wp_, but if you changed it, the queries will fail or target the wrong tables. Check your wp-config.php for$table_prefix.
Tips
- Schedule regular cleanups. Run the plugin monthly or quarterly. Set up a cron job if your plugin supports it.
- Limit post revisions in wp-config.php. Add this line to your wp-config.php file to prevent excessive revisions from being stored:
define('WP_POST_REVISIONS', 5);This limits the number of revisions per post. - Disable pingbacks and trackbacks. Go to Settings → Discussion and uncheck “Allow link notifications from other blogs (pingbacks and trackbacks).” This reduces spam and database clutter.
- Use a caching plugin. A good caching plugin reduces database queries by serving static HTML. This complements database cleaning.
- Monitor your database size. In phpMyAdmin, you can see the size of each table. If a table is unusually large, investigate what’s causing it.
Troubleshooting
- Site shows “Error establishing a database connection” after cleanup. This usually means a table was dropped or corrupted. Restore your backup immediately. If you don’t have one, contact your hosting provider.
- Plugin says “Cannot connect to database” or times out. The database might be locked by the cleanup process. Wait a few minutes and try again. If it persists, check your hosting error logs.
- Some content disappears after deleting revisions. If you deleted all revisions, you cannot restore older versions of posts. If you deleted trashed posts, they are gone permanently. Restore from backup if needed.
- Site becomes slower after cleanup. This can happen if you deleted cached transients that were still valid. The site will regenerate them over time. Clear your browser cache and wait a few hours.
- phpMyAdmin query fails with a syntax error. Double-check the table prefix. If you’re unsure, look in your wp-config.php file. Also ensure you selected the correct database before running the query.
Frequently Asked Questions
Is it safe to delete WordPress post revisions?
Yes, as long as you keep a few recent ones. Revisions are copies of your posts at different stages. Keeping 2-5 is enough for most sites.
What are transients and why should I delete them?
Transients are temporary cached data stored in the database. Expired transients are useless and clutter the database. Deleting them frees space and can improve performance.
How often should I clean my WordPress database?
For a typical blog, once a month is sufficient. High-traffic sites with lots of comments or orders might benefit from weekly cleanups.
Will database cleaning break my WordPress site?
If done correctly with a backup, it’s very safe. The risk is low, but always have a backup to restore if something unexpected happens.
Can I clean the database without a plugin?
Yes, you can use phpMyAdmin to run SQL queries, but it requires more technical knowledge. Plugins like WP-Optimize automate the process and are safer for beginners.
Does optimizing tables actually speed up my site?
Optimizing tables defragments the data and can reduce query times, especially on large databases. The improvement may be modest on small sites, but it helps overall performance and reduces server load.