Database maintenance

How to clean a WordPress database safely

Cleaning a WordPress database means removing records you have established are no longer needed. Start with visible trash and spam, then use a database tool for a specific category. Selecting every cleanup option can remove useful editing history and plugin data.

Time: 30-60 minutes Level: Intermediate
Neil McNaught, founder of BugShield and WordPress author
Written by
Written by
Updated
Updated

Decide which stored records you still need

A database stores useful history alongside records you no longer need. Cleaning it starts with deciding which is which, rather than aiming for the smallest possible number. Review familiar items such as spam and trash first, keep a recoverable backup, and leave unexplained plugin records for their owner to identify.

Set a narrow purpose for the cleanup

The database holds more than blog posts. It also contains Elementor layouts, user accounts, shop data and plugin settings. A table is a collection of records; a large or unfamiliar table is not automatically disposable.

If your aim is to recover a large amount of space, first identify which tables are largest. Deleting a handful of revisions will not resolve a rapidly growing activity log. For a slow dashboard, use the admin performance guide rather than assuming cleanup will make it faster.

1. Save a backup and establish what to keep

Follow the database export steps and keep the file away from the public website directory. Check that your backup includes all tables for this site and that you have a usable restoration method.

Ask other editors to finish their work before cleaning drafts or revisions. Revisions are previous saved versions used to recover an earlier page design or text. Keep them while a redesign or disputed edit is still in progress. On a shop, avoid removing orders, customers, sessions or scheduled actions as routine housekeeping.

2. Review trash and spam through WordPress first

  1. Open Posts > All Posts > Trash and review the items. Restore anything placed there by mistake. Select only unwanted items and use Delete Permanently.
  2. Repeat the review in Pages > All Pages > Trash. A page removed from navigation can still be needed for a campaign or future redesign, so confirm its purpose before deleting it.
  3. Open Comments > Spam. Rescue legitimate comments marked incorrectly, then delete confirmed spam. Pending comments are awaiting a decision and should not be treated as spam automatically.
  4. Check the comments Trash separately. Once these visible categories are dealt with, decide whether another cleanup category is still necessary.
WordPress Comments screen showing Spam and Trash filters above a demonstration comment
The Spam and Trash filters let you review those comment groups separately before removing records. This official example contains the default demonstration comment; the row is not evidence of spam and should not be deleted just to copy the picture. Credit: WordPress Documentation Team, CC0.

3. Use WP-Optimize for a selected database category

For expired temporary records or a deliberate revision cleanup, choose WP-Optimize from its official listing after checking compatibility, then follow the plugin installation steps. If another installed tool already performs this task, use one tool rather than scheduling overlapping cleanups.

Open WP-Optimize > Database > Optimizations. Review the selected boxes before running anything and clear the defaults. Leave caching, image compression and minification settings unchanged because they are separate features.

  1. Read the description and count for the category you intend to clean. Expired transients are temporary saved values whose expiry has passed; they are different from all transient values.
  2. Select only the expired-transient cleanup for an initial focused run. If you have instead agreed to remove revisions, select that category only and acknowledge that the saved editing history will be removed.
  3. Leave unapproved comments, metadata, unused-table removal and table optimisation unselected unless you have separately established that those actions are appropriate.
  4. Confirm your backup exists. If using the plugin's UpdraftPlus backup option, verify that the backup succeeds rather than treating the checkbox as proof. Select Run all selected optimizations once.
  5. Wait for the result and record the category and number of records removed. If the operation reports an error, save that message and stop before retrying or expanding the selection.

4. Test the site and interpret the result

Open a recent page, load its editor and verify that the intended content remains. Test your contact form with your own address. For a shop, check products and cart behaviour without clearing customer sessions. If you removed revisions, check that the current published page is intact and understand that older versions are no longer available through its revision history.

A lower record count does not guarantee an immediate reduction in the host's reported disk usage. Database engines can retain allocated space for reuse. Do not force a table rebuild merely because the storage chart has not changed; ask the host to interpret the size and advise whether maintenance is needed.

If expected content disappeared, stop new edits and investigate the exact cleanup before restoring. Use the saved backup through the planned recovery route, with special care for orders or submissions received afterwards.

5. Prevent the same data from accumulating unchecked

Review retention settings in the plugin that generates large logs or records. Set a documented retention period suited to the business instead of periodically deleting whole tables. Leave automatic cleanup disabled until you have verified a manual run and chosen categories that remain safe to remove without individual review.

Official references

The developer documents WP-Optimize's database controls. WordPress explains how revisions preserve earlier versions of content.

Get help with the next step

If a large table has an unclear purpose, ask BugShield for help before deleting it. The table name, size and plugin responsible are more useful than running another broad cleanup.

Questions answered

How to clean a WordPress database safely FAQs

Will deleting revisions remove the current published page?

A revision cleanup targets saved historical versions, not the current page. It removes that recovery history, so keep a backup and verify the current content afterwards.

Are all unused-looking tables safe to delete?

No. A plugin can retain data while temporarily disabled, and table names do not reliably establish ownership. Confirm the purpose and retention requirements before removal.

Should I delete every transient?

This procedure uses expired transients only. Deleting all temporary cached values can trigger extra rebuilding work and is a separate action that needs a reason.

Why has the reported database size barely changed?

The removed category can be small, or the database can retain allocated storage for reuse. Check the largest tables and ask the host to interpret disk usage before considering structural maintenance.

Can database cleanup fix every slow site?

No. Slow requests can come from plugins, hosting resources, external services or uncached pages. Measure the affected action and investigate its cause instead of treating cleanup as a guaranteed speed fix.

Need a hand with your WordPress site?

Tell us what happened and where you got stuck. A BugShield developer can help you work out the next step and confirm a price for the fix.

Explore help with this issue

Accessibility settings

Adjust how this site looks and moves. Your choices are saved in this browser.