Regularly cleaning your WordPress database removes orphaned metadata, expired transients, and unused entries, keeping your site fast and backups quick.
Over time, as posts are updated, created, or deleted, the wp_postmeta
table can become large and inhibit your website’s performance. In this article, we are going to show you how to clean up old, unnecessary post metadata from the wp_postmeta
table to help your WordPress site run more efficiently.
You’ll learn three easy methods:
- Plugins
- Manual Cleanup via phpMyAdmin
- Ongoing Maintenance
But First, What Gets Cleaned?
- Post revisions. Each time you update a post or page, WordPress saves a revision snapshot. Over time, these accumulate and bloat your
wp_posts
and wp_postmeta
tables, consuming storage and slowing down backups.
- Spam and trashed comments. When comments are marked as spam or moved to the trash, they remain in your
wp_comments
and wp_commentmeta
tables until you delete them permanently. Clearing these out reduces table size and improves query performance.
- Expired transients. Transients are temporary cached data stored in the
wp_options
table by plugins and themes. Once they expire, they sit unused. Removing expired transients frees up space and prevents unnecessary option lookups.
- Orphaned postmeta and usermeta entries. These are metadata rows in
wp_postmeta
or wp_usermeta
that reference posts or users no longer in the database. Deleting orphaned entries streamlines your tables and eliminates dead data.
Method 1: Plugins
Using a plugin is the simplest way to clean your database without touching SQL. Install one of these in minutes:
- Navigate to Plugins > Add New, search for “WP-Optimize,” and click Install Now.
- Activate the plugin, then go to WP-Optimize > Database and click the Run all selected optimizations button.
- Install and activate Advanced Database Cleaner from the plugin directory.
- Under Database Cleaner, select old revisions, transients, and orphaned metadata, then click Clean.
- Go to Plugins > Add New, search for “WP Sweep,” and hit Install Now.
- Activate the plugin, then visit Tools > Sweep.
- Review the list (Revisions, Auto drafts, Trashed comments, Transients, Orphaned metadata, etc.), click Sweep next to each item to remove it, or use Sweep All at the top.
Each plugin shows how many items it removed. For hands-off maintenance, enable its scheduling feature to run monthly.
Method 2: Manual Cleanup
Step 1. Before continuing, make a backup of your database. This step is critical as we are making mass deletions that can potentially break things on your site.
Step 2. Log into cPanel.
Step 3. Next, scroll down to the Databases section and click on phpMyAdmin.
Step 4. Within PHPMyAdmin, select your WordPress database on the left side column. If you are unsure of the database that your WordPress site is using, follow our guide on finding your WordPress database name.
Step 5. At the top of the page, click on SQL.
Inside the text area, enter the following:
SELECT * FROM wp_postmeta pm LEFT JOIN wp_posts wp ON wp.ID = pm.post_id WHERE wp.ID IS NULL;
DELETE pm FROM wp_postmeta pm LEFT JOIN wp_posts wp ON wp.ID = pm.post_id WHERE wp.ID IS NULL;
This cleans the wp_postmeta
table.
For the wp_usermeta
(orphaned entries) table, enter the following SQL code:
SELECT * FROM wp_usermeta um LEFT JOIN wp_users wu ON wu.ID = um.user_id WHERE wu.ID IS NULL;
DELETE um FROM wp_usermeta um LEFT JOIN wp_users wu ON wu.ID = um.user_id WHERE wu.ID IS NULL;
For the wp_commentmeta
(orphaned comment metadata) table, enter the following SQL code:
SELECT * FROM wp_commentmeta cm LEFT JOIN wp_comments wc ON wc.comment_ID = cm.comment_id WHERE wc.comment_ID IS NULL;
DELETE cm FROM wp_commentmeta cm LEFT JOIN wp_comments wc ON wc.comment_ID = cm.comment_id WHERE wc.comment_ID IS NULL;
For the wp_options
(especially for expired transients) table, enter the following SQL code:
DELETE FROM wp_options WHERE option_name LIKE '_transient_%' AND option_name NOT LIKE '_transient_timeout_%';
DELETE FROM wp_options WHERE option_name LIKE '_transient_timeout_%' AND option_value < UNIX_TIMESTAMP();
Step 7. Then, press the Go button on the bottom right.
PHPMyAdmin will either let you know how many items it removed or state that the result is empty which means that nothing was found. Congratulations! Your wp_postmeta
table has now been cleaned up.
Method 3: Ongoing Maintenance
To keep your WordPress database lean and fast, schedule regular cleanups as part of your site’s maintenance routine. For high-traffic or content-heavy sites, consider a weekly schedule. For smaller sites, monthly may be enough.
Most optimization plugins allow you to automate these cleanups on a weekly or monthly basis. This helps prevent long-term bloat and keeps your site running at peak performance with minimal effort.
Common Questions Answered
How often should I clean up my WordPress database?
It depends on how often your site is updated. If you publish new posts regularly or run a high-traffic site, a monthly cleanup is ideal to keep performance sharp. For smaller sites with fewer updates, a quarterly schedule is often enough to prevent unnecessary bloat.
Is it safe to clean the database?
Yes, as long as you take proper precautions. Always back up your site and database before making any changes. If possible, test your cleanup process on a staging site to ensure nothing breaks. Plugins often have built-in safety nets, but manual deletions require more care.
What do I do if something breaks during the cleanup?
Restore your site from the backup you made before starting. This will undo any unintended changes and bring your database back to its previous working state. If you’re unsure how to do this, most hosting providers — including InMotion Hosting — offer tools and support to help with site recovery.