Skip to content

How to Clean Up a WordPress Site Safely With WP-Optimize and WP-CLI

How to Clean Up a WordPress Site Safely With WP-Optimize and WP-CLI

You can clean up a WordPress site safely in about an hour. Back it up first and then remove one kind of clutter at a time. The safe targets are unused plugins and themes, post revisions, spam, expired transients and orphaned data.

We tested every step on a WordPress 7.1.3 test store with WooCommerce 11.2 in October 2026. We saved a database backup before each run and restored it between tests.

This guide shows two routes, the WP-Optimize plugin and WP-CLI. It also covers media library cleanup that keeps files in use, database optimization, and how to check the site afterward.

Back Up Your WordPress Site Before You Clean Up

Back up the database and the uploads folder before you delete anything. A cleanup removes rows and files for good, and a backup is the only way to undo a mistake. The WordPress backup guide says the database holds every post and comment. Losing it means losing your content.

On a server with WP-CLI, the database backup is one command. Run it from the folder that holds wp-config.php, and keep the file outside the public web folder.

WP-CLI commands that export and restore a WordPress database backup
Terminal output from our WordPress 7.1.3 test site

Copy the wp-content/uploads folder too, because the database does not contain your image files. If you run a store, our guide to backing up the WooCommerce database covers UpdraftPlus, phpMyAdmin and WP-CLI backups.

We exported the backup, ran each cleanup, and imported the backup again to get the same dirty site back. The restore worked every time, and that is how you should test yours before you trust it.

What Is Safe to Remove in a WordPress Cleanup?

Safe items are the ones WordPress rebuilds or that you no longer use. Inactive plugins and themes, post revisions, spam comments, expired transients and orphaned metadata all qualify after a backup. Unattached media does not qualify by itself, because a file can be in use while WordPress still lists it as unattached. If WooCommerce is the plugin you no longer need, remove WooCommerce completely.

Use the table below as your checklist. Each row names the one thing to check before you remove that item.

Table of WordPress cleanup items that are safe to remove and what to check first

Never delete core tables, the active_plugins option, or any option that belongs to a plugin you still run. Removing those rows can break the whole site, even though a quick SQL snippet makes it look easy. The methods below use plugin tools and WP-CLI commands that only target the clutter types in the table.

Three steps for a safe WordPress cleanup from backup to verification

Method 1: Clean Up a WordPress Site With WP-Optimize

The easiest way to clean up a WordPress site is the WP-Optimize plugin. Its Database tab handles WordPress database cleanup with a live count for each kind of clutter, and one button runs everything you ticked. The plugin page lists more than 1 million active installations as of October 2026.

We used WP-Optimize 4.7.0. We loaded the test store with 73 revisions, 25 trashed posts, 11 auto-drafts, 40 spam comments, expired transients and 5 orphaned post meta rows.

  1. Install the plugin: Go to Plugins > Add Plugin and search for WP-Optimize. Click Install Now and Activate.
  2. Open the Database tab: Go to WP-Optimize > Database and stay on the Optimizations tab. Each row shows how much clutter it found.
  3. Choose what to run: Tick the rows you want. Our test run used revisions, auto-drafts, trashed posts, spam comments, expired transients and post metadata, plus table optimization.
    WP-Optimize database optimizations with expired transients and post metadata selected
  4. Confirm the warning: Click Run all selected optimizations. Because we ticked post metadata, the plugin asked for a confirmation first. Tick the box and click Apply optimization.
    WP-Optimize warning before it removes orphaned post metadata
  5. Read the results: Each row now shows what it removed. We saw 73 revisions, 11 auto-drafts, 25 trashed posts, 40 spam comments and 124 transient options deleted.
    WP-Optimize results after you clean up a WordPress site showing deleted rows

The database went from 7,696 KB to 6,544 KB in that run. The WP-CLI run below ended lower. It also cleared transients that had not expired yet, and a repeat run in the WP-CLI section shows how much that adds.

Note: The Remove unapproved comments row is ticked by default. If you moderate comments, untick it. A comment waiting for approval is not spam. Our test store had none and nothing was lost.

One quirk is worth knowing. The post metadata row reported 0 deleted, yet a database query afterward found no orphaned rows left. Check any count you care about with a query instead of trusting one result line.

Method 2: Clean Up a WordPress Site With WP-CLI

You can clean up a WordPress site from the command line with WP-CLI. You can clean WordPress database tables without opening wp-admin. Each command below targets one kind of clutter. You can run them one at a time and check the site between them. They need shell access to the server.

  1. Remove old revisions: Count them first and then delete them. The --force flag skips the trash, as the wp post delete reference explains.
    WP-CLI commands that count and delete WordPress post revisions
  2. Clear spam and expired transients: Delete the spam comments and then run the transient command from the wp transient delete reference. Then optimize the tables and check the size.
    WP-CLI commands that clear spam and expired transients and optimize the WordPress database

These are the commands behind the two screenshots:

wp post list --post_type=revision --format=count
wp post delete $(wp post list --post_type=revision --format=ids) --force

wp comment delete $(wp comment list --status=spam --format=ids) --force

wp transient delete --expired

Run the same pattern for trashed posts and auto-drafts by changing --post_status:

wp post delete $(wp post list --post_status=trash --post_type=post --format=ids) --force
wp post delete $(wp post list --post_status=auto-draft --post_type=post --format=ids) --force

Each of those lists IDs first and passes them to the delete command. If no IDs match, WP-CLI prints a usage message instead of deleting anything, which we confirmed on an empty list. That message is harmless.

Our test store had 73 revisions, 25 trashed posts, 11 auto-drafts and 40 spam comments. The commands cleared all of them. The database fell from 7,696 KB to 5,552 KB. We also ran wp transient delete --all. It removed 54 more transients that had not yet expired. This total is lower than the plugin run.

A repeat run on a fresh copy of the dirty store shows the effect. The database started at 8,016 KB. The expired-only command left it at 6,960 KB. Adding wp transient delete --all took it to 5,920 KB. So unexpired transients held about 1 MB.

Use --all with care on a busy store. Transients are temporary cache entries. WordPress and plugins rebuild them. The first page loads afterward can be slower.

How to Clean Up the WordPress Media Library Without Deleting Files in Use

To clean up WordPress media library clutter safely, check where each file is used. Do not delete files because WordPress calls them unattached. In our test, three images that were live on the site still showed as unattached in the Media Library.

We set up three images in real use. One sat in a post, one was a product’s featured image, and one was in a product gallery. Five more images were unused.

WordPress Media Library listing images in use as unattached

That is why a database query for attachments with a parent of 0 is dangerous. It matches every featured image and every file inserted without a parent, and deleting those removes pictures from live pages.

A scanner that reads your content is safer. We tested the free Media Cleaner plugin, which lists more than 90,000 active installations as of October 2026. Its scan left the three images in use alone and flagged the five unused files, plus one old site icon the site no longer uses.

Media Cleaner scan results listing WordPress media files not found in content

The plugin asks you to confirm a safety checklist before it scans. It says deleted files go to a trash first, and that emptying the trash is permanent.

We tested that trash. We moved one flagged image to the trash, and its URL returned a 404. After we recovered it, the URL returned 200 again, and the site worked as before.

Warning: Media Cleaner’s own notice says a file can be flagged as unused while a builder, template or script still needs it. Look at each flagged file, and recover anything you doubt before you empty the trash.

Remove Unused Plugins and Themes

Delete every plugin and theme you no longer use. Each one adds files that need updates, and many leave data behind in your database. The Inactive filter on the Plugins screen lets you review all of them in one list.

Inactive plugins list in WordPress with Delete links for unused plugins

You do not need a plugin to remove old themes. Go to Appearance > Themes, open the theme and click Delete. Our guide on how to delete a theme in WordPress walks through it.

On the command line, list first and delete by name:

wp plugin list --status=inactive
wp plugin delete hello-dolly akismet

wp theme list --status=inactive
wp theme delete twentytwentyone twentytwentythree

Always list before you delete. On our test site, wp plugin delete removed an active plugin as well. The --status=inactive list is your safety check. WP-CLI refused to delete our active theme and printed a warning instead.

Keep one default theme installed as a fallback, and keep the parent of any child theme you use.

WordPress Database Optimization After a Cleanup

Optimize the database tables after a large cleanup to reclaim the space the deleted rows used. The wp db optimize reference says the command runs the mysqlcheck utility with the optimize option. WP-Optimize does the same job from its Optimize database tables row.

On our test site, the command printed a note for each table: “Table does not support optimize, doing recreate + analyze instead”. That is how MySQL reports an optimize on an InnoDB table. The MySQL manual says it rebuilds the table to update index statistics and free unused space.

Every table ended with status OK. The note is not an error. Run the optimize step once at the end. Do not run it after each delete because each run rebuilds every table.

Tables left by plugins you removed stay in the database. List them with wp db tables --all-tables-with-prefix and compare the result with wp db tables. We found tables left by plugins that were removed from our test site long ago.

One WP-Optimize leftover took 64 KB. The space is small. Leave those tables alone unless you are sure the plugin is gone for good. Dropping a table is not reversible without your backup, and the saving is rarely worth the risk.

How to Check Your Site After a Cleanup

Check the site right after the cleanup, while your backup is still fresh. Load the home page, a post, the shop, a product and the checkout, and look at the media on each one. If anything is wrong, restore the backup first and investigate afterward.

A restore is quick when you have tested it. We imported our backup file with wp db import, and the products, posts and images came back as they were. Keep the backup file for a few days, because some problems only show up when a visitor hits an old page.

Use this short list after every run:

  • Front end: Open a post with an image, a product page and the cart. Check that no image is missing.
  • Admin: Open the Plugins screen and the Media Library, and confirm nothing you need is gone.
  • Database: Run wp db check. On our test site it ended with “Database checked” and OK for every table.
  • Errors: Look at your PHP error log for new notices. A fatal error after deleting a plugin points to a missing dependency.

If a plugin conflict shows up, our guide to checking for plugin conflicts shows how to isolate it. A slow site after cleanup usually has other causes, which why WordPress is slow explains.

How Often Should You Clean Up a WordPress Site?

Clean up a WordPress site about once a month, and cap revisions so they stop piling up. The wp-config.php reference says revisions are on by default and that you can set a maximum number per post. It also says the trash empties after 30 days by default.

We tested the revision cap. This command added the constant for us:

wp config set WP_POST_REVISIONS 5 --raw

After we updated one post eight times, WordPress kept only 5 revisions. Pick a number that fits how you edit. WP-Optimize also offers scheduled cleaning on its Settings tab, and it was disabled by default on our install.

A monthly check takes minutes once the process is routine. If you would rather not do it yourself, our WordPress maintenance services cover regular database cleanup and updates.

Conclusion

To clean up a WordPress site safely, back up first and then remove one kind of clutter at a time. WP-Optimize handles revisions, spam, transients and orphaned data from one screen, and WP-CLI does the same faster on a server. Our test cleanup cut the database from 7,696 KB to as low as 5,552 KB without touching a single page.

Treat media separately. Never delete files because they are unattached, and use a scanner such as Media Cleaner with its trash as your safety net. Finish with an optimize run and a short check of the site.

Frequently Asked Questions (FAQs)

Q1. Is it safe to delete post revisions in WordPress?

Yes, after a backup. Revisions are old saved versions of a post. Deleting them removes edit history but not the live content. On our test store, deleting 73 revisions left every post and page unchanged.

Q2. Will cleaning the database make my site faster?

We did not measure page speed. We cannot promise it. We measured size, and the test database shrank from 7,696 KB to as low as 5,552 KB. Slow pages often come from other causes, such as hosting or heavy plugins.

Q3. Is it safe to delete all transients in WordPress?

Mostly yes. Transients are temporary cache entries, and the wp transient delete reference lists both an expired-only option and an all option.

Our --all run removed 54 extra entries without errors, and pages loaded normally. Expect the first few page loads to rebuild them.

Q4. What should you never delete from the WordPress database?

Never delete core tables, the active_plugins option, or options your active plugins use. Remove only clutter types you can name, such as revisions, spam and expired transients. Use a plugin or WP-CLI command for each type, and keep a backup.

Q5. What is orphaned data in WordPress?

Orphaned data is a row that points to something that no longer exists. A common case is post meta whose post was deleted. Our test store had 5 such rows, and WP-Optimize flagged all five before it cleaned them.

Q6. Do I need a plugin to clean a WordPress database?

No. WP-CLI covers revisions, trash, spam, transients and optimization with the commands in this guide. A plugin such as WP-Optimize is easier if you have no shell access, and it shows live counts before you delete anything.

Yash Kapoor
Yash Kapoor

Yash Kapoor is the founder and WordPress expert at DevDiggers, where he builds WordPress and WooCommerce plugins for loyalty programs, point-of-sale systems, digital wallets and affiliate management. He writes about developer tools, site performance and the technical side of running a WordPress store. He draws on years of building and maintaining production plugin codebases.

Leave a Reply

Your email address will not be published. Required fields are marked *