How to perform a WordPress database cleanup

Last updated on April 18, 2022 | By

Much the same as your front end, you should also strive to optimize your backend. Cleaning and optimizing your database is a good practice.

To help on optimizing a WordPress database, here are 4 ways to clean it. If you already have a cache or speed optimization plugin running, you can clean using Wp CLI, Advanced Database Cleaner, or optimize with PhPMyAdmin.

⚠️ Be careful when cleaning your database. Consider backup your database before any changes.

Clean database using Wp Rocket plugin

Wp Rocket provides a simple interface for deleting unused database pieces such as post revisions, auto-draft wordpress posts/pages, trashed posts, comments, and plugin transients.

wp rocket database cleanup
Wp Rocket database full database cleanup options(click image to enhance)

Consider buying Wp Rocket with our reflink.

Clean database using Wp Optimize plugin

Wp Optimize is a free plugin that also offers database cleanup, with more advanced and extra options.

optimize database wp optimize
Wp Optimize WordPress database cleanup options (click image to enhance)

Wp Optimize also can schedule database cleanups:

optimize database wp optimize 3
Wp Optimize WordPress database cleanup general options(click image to enhance)

List of tables and plugins that may be related to that table, helping you choose if it’s a used table:

optimize database wp optimize 2
List of tables and plugins that probably uses that table(click image to enhance)

Clean database without a plugin with Wp Cli

Wp Cli is a free command line WordPress utility tool. To install Wp CLI follow this guide.

For the full hosting companies with WP CLI installed, check here. If you’re using a WordPress Bitnami version, WP CLI is already available. 

The commands for optimizing the database are:

Delete all spam comments(Marked as spam).

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

Delete all comments marked as hold.

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

Delete Expired Transients(Temporary plugin data), safe to exclude assuming it’s not needed.

$ wp transient delete --expired

Optimizes the database

wp db optimize

Delete all posts in the trash

$ wp post delete $(wp post list --post_status=trash --format=ids)

Clean database with Advanced Database Cleaner plugin

Advanced Database Cleaner plugin offers all majors options to clean up the database, including orphaned items:

optimize database advanced db cleaner
Advanced DB cleanup plugin main options(click image to enhance)

List of scanned tables(Pro version shows plugins belonging to each table):

optimize database advanced db cleaner 2

Optimize database with PhpMyadmin

To optimize the WordPress database with PHPMyAdmin, log into your PHPMyAdmin, go to your databases, click on one database, and check all the tables of the DB you want to optimize, then click on the bottom “Optimize table”. It will optimize all the tables selected.

optimize db php my admin

Get your Core Web Vitals optimized

Leave a Comment