Cleaning up a WordPress database means removing unnecessary data—like post revisions, spam comments, and transients—to reduce its size and improve site performance. You can do it safely by using a plugin (e.g., WP-Optimize) or by running a few SQL commands via phpMyAdmin, but always back up first. This guide explains what causes bloat, how to clean it, and what to avoid.
Why Your WordPress Database Gets Bloated
WordPress stores everything in a MySQL database: posts, pages, comments, user data, plugin settings, and more. Over time, it accumulates data you don’t need. The main culprits:
- Post revisions: Every time you save a draft, WordPress stores a revision. A post edited 50 times has 50 extra rows in the
wp_poststable. - Spam and trashed comments: Akismet and other filters can leave thousands of spam comments in the database.
- Transients: Cached data stored by plugins and themes with an expiration date. Expired transients are not always cleaned automatically.
- Orphaned data: When you delete a plugin, its tables often remain. Same with shortcodes from deactivated themes.
- Pingbacks and trackbacks: These can pile up, especially on older posts.
- User meta and post meta: Plugins that store extra data for each user or post can leave behind thousands of rows.
How to Clean Your Database (Step-by-Step)
Here are the two main methods, from safest to most technical.
Method 1: Using a Plugin (Recommended for Most Users)
- Back up your database. Use a plugin like UpdraftPlus or your hosting control panel (cPanel → phpMyAdmin → Export). Store the backup off-site.
- Install a cleanup plugin. Popular options include WP-Optimize, Advanced Database Cleaner, and WP-Sweep. They are free and widely used.
- Run a scan. The plugin will show you how much space each type of data takes.
- Select what to clean. Typically you can remove post revisions, auto-drafts, trashed items, spam comments, and expired transients. Leave any data that looks unfamiliar.
- Run the cleanup. The plugin will execute the deletions and show you how much space you saved.
Method 2: Using phpMyAdmin (For Advanced Users)
If you’re comfortable with SQL, you can run queries directly. Always back up first!
- Log into your hosting control panel and open phpMyAdmin.
- Select your WordPress database (usually the one with
wp_prefix). - Click the “SQL� tab and run queries like:
DELETE FROM wp_posts WHERE post_type = 'revision';
DELETE FROM wp_comments WHERE comment_approved = 'spam';
DELETE FROM wp_options WHERE option_name LIKE '_transient_%' AND option_name NOT LIKE '%_transient_timeout_%'; - After each query, check the number of affected rows to confirm it worked.
What to Clean vs. What to Leave Alone
Not all data is junk. Here’s a quick guide:
- Safe to delete: post revisions, auto-drafts, trashed posts/pages/comments, spam comments, expired transients, pingbacks/trackbacks, orphaned plugin tables (if you’re sure you won’t use the plugin again).
- Do NOT delete: active plugin settings, user data (unless you’re removing a specific user), options that are not transients, and any table you don’t recognize. Deleting the wrong row can break your site.
Common Mistakes and How to Avoid Them
- Skipping the backup: One wrong query can wipe out your entire site. Always back up before cleaning.
- Deleting all transients: Some plugins store non-expiring transients (e.g., license keys, API tokens). Use a plugin that only removes expired ones, or filter your SQL to exclude those without a timeout.
- Cleaning too often: Running a cleanup every day can slow down your site and cause unnecessary load. Once a month is enough for most sites.
- Using untrusted plugins: Some “database cleaner� plugins have been known to delete too much. Stick to well-reviewed, popular ones.
- Ignoring the database prefix: If your prefix is not
wp_, your SQL queries will fail. Check yourwp-config.phpfile.
Should You Optimize Tables Too?
Optimizing tables (using OPTIMIZE TABLE or the plugin’s “Optimizeâ€� feature) defragments the data and reclaims unused space. It’s safe and recommended after a cleanup. In phpMyAdmin, you can select all tables and choose “Optimizeâ€� from the dropdown. This doesn’t delete anything—it just reorganizes the data for faster access.
How Often Should You Clean?
It depends on your site’s activity. A blog with daily posts and comments might need monthly cleaning. A small static site can go six months or more. Signs you need a cleanup:
- Your database is over 100 MB (for a typical blog).
- Your site is slow, especially in the admin area.
- You see “database connection error� messages.
- Your hosting provider warns about database size limits.
Frequently Asked Questions
Will cleaning my database speed up my site?
It can, but not always dramatically. A smaller database means faster queries, but your site’s speed is also affected by hosting, caching, and image sizes. If your database is bloated (e.g., 500 MB), you’ll likely see improvement. If it’s already small, the effect will be minimal.
Can I delete old post revisions without breaking anything?
Yes. Post revisions are just snapshots of your edits. Deleting them does not affect the published post. You can limit future revisions by adding this to your wp-config.php: define('WP_POST_REVISIONS', 5); (keeps only the last 5).
What is the safest way to clean a WordPress database?
The safest way is to use a reputable plugin like WP-Optimize, which lets you preview what will be deleted and automatically backs up before cleaning. If you use raw SQL, always export a backup first and test the query on a staging site if possible.
Why is my database so large even though I have few posts?
Often it’s due to plugins. For example, WooCommerce stores order data, analytics, and session data. Other plugins may store logs, caches, or tracking data. Use a plugin to scan for large tables and identify what’s taking up space.
Can I clean the database without a plugin?
Yes, you can use phpMyAdmin to run SQL queries, but it’s riskier. You need to know exactly which tables and rows to delete. For non-technical users, a plugin is strongly recommended.
Does cleaning the database affect my SEO?
No, not directly. As long as you don’t delete published posts, pages, or settings, your SEO is unaffected. In fact, a faster site can improve user experience, which indirectly helps SEO.
Related
Formula used
in t | Finish time | min | 50 | 0 in d | Distance | km | 10 | 0 | 100000 out pace | Average pace | min/km | t / d | 2 out speed | Average speed | km/h | d / (t / 60) | 2
Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Philips DST7060/20 7000 Series Iron: Specs & Fit
- Coal Fueled Jet Aircraft: Could It Work?675 L
- Remote Desktop on Raspberry Pi: How to Enable It
- Word Select Last Line in Paragraph?
- Google Assistant IFTTT Recipes: 7 Useful Automations
- OLED TVs in Dark Rooms: Why They Look Better and When They Don’t