If your WordPress site has been running for a while, chances are the database has quietly accumulated a lot of unnecessary data. Old post revisions, spam comments, and expired transients can pile up in the background and gradually affect performance. One of the simplest ways to keep your website running efficiently is to clean up the WordPress database and remove data that no longer serves a purpose.
Many tutorials recommend installing a database optimization plugin for this task. Plugins can certainly help, but they also add more code and background processes to your website. In many cases, you can accomplish the same cleanup using a few safe SQL queries or a couple of WP-CLI commands.
This guide walks through practical steps to clean up the WordPress database without plugins, helping you remove common sources of database bloat and keep your website running lean and efficient.
Why Database Bloat Slows Websites?
The WordPress database stores nearly everything on your website, including posts, pages, comments, settings, and temporary cached data. As your website grows, the database naturally grows too.
The problem happens when unnecessary data accumulates.
Some common examples include:
- Post revisions created every time you update an article
- Spam and trashed comments that remain in the database
- Expired transients used by themes and plugins for temporary caching
- Orphaned metadata left behind after deleting plugins or content
When the database becomes bloated, queries take longer to process. This can lead to slower page loads, higher server resource usage, and inefficient backups.
Regular maintenance helps keep things running smoothly. Learning how to clean up the WordPress database manually is one of the simplest ways to maintain performance.
Backup First (Always!)
Before making any changes to your database, create a full backup first. Do not overlook this, as a single wrong query can erase data.
This step protects you in case something goes wrong. If a query removes more data than expected, you can restore your website quickly.
There are several ways to back up a WordPress database:
Using phpMyAdmin
- Log in to your hosting control panel.
- Open phpMyAdmin.
- Select your WordPress database.
- Click Export and download the SQL backup.
Using WP-CLI
If you have SSH access, you can run:
wp db export backup.sql
Once the backup is saved, you can safely proceed with the cleanup.
Remove Old Post Revisions
WordPress automatically stores revisions whenever you update a post or page. This is useful for restoring earlier versions of content, but over time revisions can grow into hundreds or even thousands of entries.
These revisions are stored in the wp_posts table.
To remove them using SQL, run this query:
DELETE FROM wp_posts
WHERE post_type = "revision";
If you are using WP-CLI, the command is even simpler:
wp post delete $(wp post list --post_type='revision' --format=ids)
After deleting old revisions, you can limit how many WordPress keeps in the future by adding this to wp-config.php:
define('WP_POST_REVISIONS', 5);
This keeps only the latest five revisions per post and prevents the database from filling up again.
Delete Trashed and Spam Comments
Spam comments and deleted comments often remain stored in the database long after they are removed from the website interface.
To delete them permanently using SQL:
Remove Spam Comments
DELETE FROM wp_comments WHERE comment_approved = 'spam';
Remove Trashed Comments
DELETE FROM wp_comments WHERE comment_approved = 'trash';
If you prefer WP-CLI, you can run:
wp comment delete $(wp comment list --status=spam --format=ids)
Clearing out these comments can noticeably reduce the size of the comments table on websites that receive frequent spam
Clear Expired Transients
Transients are temporary cached values used by WordPress, themes, and plugins. They help speed up operations by storing short-term data.
The issue is that expired transients sometimes remain in the database even after they are no longer needed.
To remove expired transients using SQL:
DELETE FROM wp_options
WHERE option_name LIKE '_transient_%'
AND option_name NOT LIKE '_transient_timeout_%';
With WP-CLI, the process is easier:
wp transient delete –expired
Cleaning out these temporary records helps keep the options table lean and improves query performance.
Optimize Tables via phpMyAdmin or WP-CLI
After removing unnecessary data, it is a good idea to optimize the database tables. Optimization reorganizes the table structure and reclaims unused storage space.
Using phpMyAdmin
- Open your WordPress database.
- Scroll to the bottom of the tables list.
- Select Check All.
- Choose Optimize Table from the dropdown menu.
Using WP-CLI
You can optimize the entire database with one command:
wp db optimize
This step ensures your database runs efficiently after the cleanup process.
Automating Cleanup with Cron
Manually running database cleanup works well, but automation can make maintenance easier.
You can schedule periodic cleanup using a server cron job and WP-CLI.
For example, a monthly cleanup task might look like this:
0 3 1 * * wp transient delete –expired
This runs on the first day of every month at 3 AM and removes expired transients automatically.
You can combine multiple commands in a script to remove revisions, clear transients, and optimize the database regularly.
Automation helps ensure your database stays healthy without constant manual work.
Final Thoughts
Keeping your database tidy is one of the simplest ways to maintain a fast and efficient WordPress site. Over time, revisions, spam comments, and expired transients can quietly accumulate and add unnecessary weight to your database.
Instead of relying on plugins, you can clean up the WordPress database directly using SQL queries or WP-CLI commands. This approach keeps your website lean, gives you precise control over what gets removed, and avoids adding extra tools that may not be needed.
Make database maintenance part of your regular website routine. When you periodically clean up the WordPress database, you help ensure faster queries, smaller backups, and a smoother experience for visitors.
Feel free to drop a comment if you have any questions. If this guide was helpful to you, you may want to check out our post about adding IndexNow Support to WordPress without a Plugin.