Much like your front end, you should also strive to optimize your back end. 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.
Clean database using Wp Optimize plugin
Wp Optimize is a free plugin offering database cleanup, with more advanced and extra options.
Wp Optimize also can schedule database cleanups:
List of tables and plugins that may be related to that table, helping you choose if it’s a used table:
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.
Wp Cli might be available on your Wordpress install. Check for the hosting companies with WP CLI installed. 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 a 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:
List of scanned tables(Pro version shows plugins belonging to each table):
Optimize database with PHPMyAdmin
To optimize the Wordpress database with PHPMyAdmin, log into your PHPMyAdmin, go to your databases, click on one database, 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.
Get your WordPress Core Web Vitals optimized and your pages faster!