Blog / WordPress /

Cleaning up a WordPress database without breaking anything

A cleanup plugin shows you a red number next to “overhead” and a big friendly Optimise button. Tempting. Click, and the site is tidy.

Except the database isn’t a rubbish bin. It holds your posts, settings, users and plugin data, and deleting rows on a plugin’s say-so can remove something the site still needs. A tidy-up can help, but only once you know what’s taking space and have a reliable way back.

Start with a tested backup, name the specific problem and change one thing at a time. Revisions and expired transients aren’t the same as plugin tables or options nobody can explain. And if the site is slow, database size may not be the cause at all. Measure before you clean.

Understand what is in the database

WordPress stores content and configuration across several tables. Posts and pages, revisions, comments, user records, plugin settings and temporary data all have different purposes. Plugins may create their own tables and options, and some leave them behind when removed. Unfamiliar data isn’t proof that it’s safe to delete.

Begin by checking the database size and which tables account for it. Your hosting panel, database management tool or maintenance plugin may show this information. Look for unusually large tables, but work out who owns each one before acting. A table belonging to an active plugin may be essential, even if its name means nothing to you.

A large database isn’t automatically a performance problem for visitors. Revisions make backups bigger, but they don’t necessarily slow every page. Autoloaded options matter more, because WordPress may load them on each request. Slow queries, weak hosting or a plugin doing expensive work often matter more than total size.

Back up and test restoration

Take a complete database backup before cleanup and keep it away from the live server. If the site matters, restore it somewhere safe to prove it works. A backup file that exists is not proof that restoring will work. On client sites, I won’t start a cleanup until I’ve seen that restore succeed.

Know what the backup includes. Some tools back up the database and files separately, and a database-only copy will not restore uploaded images or theme code. Record the time and location of the backup, and make sure the person doing the cleanup can access it if something goes wrong.

For a high-risk or busy site, perform the cleanup on staging first. Refreshing staging and later pushing its database to production can overwrite new posts, orders or form submissions, so understand the deployment method. For simple database maintenance, a targeted query or tool may be safer than copying the whole staging database back.

Clean only what you can identify

Revisions and trash

Old revisions may be removed if the site owner no longer needs them, but decide how many should remain going forward. Revisions are useful recovery points. Set a reasonable limit rather than disabling them entirely, and preserve any content history required by the organisation.

Expired transients

Transients are temporary cached values. Expired entries can usually be removed and regenerated, but use a trusted tool and understand what it will delete. Do not clear all options just because the word “transient” appears in a report.

Spam and trashed content

Spam comments and content in the trash can be cleared after review. Check that nothing was moved there accidentally, and understand how the cleanup tool treats attachments and related records.

Plugin leftovers

This is where caution matters most. An uninstalled plugin may leave data behind on purpose, or another extension may still rely on it. Check the plugin’s documentation and find its tables or options. If you can’t establish who owns the data, don’t delete it on a live site on a guess.

Use a safe process

First note the current database size and the issue you are trying to solve. Take and verify the backup. Choose one known category of data and use a reputable tool or a reviewed command. And never run a deletion query copied from a forum without understanding the table prefix, the conditions and how many rows it touches.

After cleanup, test the front end and WordPress admin. Check the editor, forms, search, checkout if present, and the plugin features related to the data you removed. Look at logs and error messages. Compare the original issue rather than assuming a smaller database means a faster site.

Keep a record of what was deleted, how and when. If something breaks, that note helps identify the change. If you perform routine maintenance, schedule it around the site’s activity and coordinate with the people who publish content or process orders.

Check autoloaded options separately

If you suspect autoloaded options are contributing to slow requests, inspect them as a performance question rather than treating them as general clutter. Identify which options are loaded, how large they are and which active plugin or theme owns them. A high count or a large value is a reason to investigate, not an instruction to delete it. Removing an option that an active component expects can cause errors or reset settings. If ownership is unclear, ask the developer or plugin maintainer before changing it. After any safe adjustment, compare the same page or request under similar conditions and check that the relevant feature still works. This keeps database maintenance tied to the original problem instead of turning it into a hunt for the smallest possible database.

When not to clean

If the database is functioning and the cleanup target is unclear, leave it alone until you can investigate. If there is a performance issue, profile it. If backups are slow, database size may be relevant, but so may file size or hosting limits. If the site has sensitive records, follow its retention rules before deleting anything.

Maintenance should reduce risk, not create it. A clean database isn’t a trophy, and deleting the most rows isn’t the goal. Keep what the site needs and remove only what you can explain.

Before your next cleanup, can you name the exact data you plan to remove, and prove your backup can bring it back?