Urgent.News

What's breaking now, across thousands of outlets.

Tech

`wp db check` / `wp db optimize` — the database health commands that get overlooked

A WordPress database doesn't tidy itself up over time. Spam comments pile up, expired transients linger, post revisions accumulate, and tables left behind by uninstalled plugins never quite go away. All of that adds up to bloated tables, and occasionally to actual table corruption. This is territory the admin dashboard barely shows you — but WP-CLI reaches it directly with two short commands: wp…

WordPress databases don't clean themselves over time. As you add and delete content, spam comments accumulate, expired transients linger, and post revisions pile up. Tables left behind by uninstalled plugins can also hang around, all of which adds up to bloated tables and occasionally even corrupted data. While the admin dashboard doesn't highlight these issues, WP-CLI provides two simple commands to address them: wp db check and wp db optimize.

WP-CLI's wp db subcommands work directly with the MySQL or MariaDB database WordPress relies on, bypassing the admin interface entirely. To run these commands, just specify the wp-config.php credentials that WP-CLI reads automatically.

wp db check verifies the health of each table by running the equivalent of mysqlcheck --check against every table, reporting their status. If any table comes back corrupt, database queries may start failing in specific ways, like a single page going blank or a post refusing to save. Running wp db check regularly can catch these issues before they cause noticeable problems.

wp db optimize, on the other hand, defragments tables and reclaims disk space. Tables with many row deletions and updates can become fragmented over time, with deleted rows still taking up space on the disk. OPTIMIZE TABLE rebuilds the table, refreshing it and freeing up that space. The specific behavior depends on the storage engine.

WordPress uses the default InnoDB engine, which handles OPTIMIZE TABLE internally by rebuilding the table, similar to ALTER TABLE ... FORCE. Older MyISAM tables, however, don't automatically reclaim space, and that disk space only gets released after OPTIMIZE TABLE runs. If you have a mixed-engine setup, it's worth checking with wp db query SHOW TABLE STATUS to ensure all your tables are optimized properly.

Both wp db check and wp db optimize are lightweight commands, taking only a few seconds to a few minutes to complete. They're harmless to run at any time, but they're most beneficial when executed during low-traffic periods, such as after bulk deletions, before major updates, or as part of routine maintenance. By adding these commands to your weekly or monthly maintenance cycle, you can catch bloated tables and corrupted data before they cause significant issues.

In rare cases, if wp db check reports a corrupted table, you can use wp db repair (equivalent to mysqlcheck --repair) to attempt recovery. However, repair isn't guaranteed to restore the data perfectly, and you should always take a fresh backup before attempting any repairs. A backup taken moments before corruption is far more valuable than one taken after the fact.

Checking database health with WP-CLI's commands is a simple yet crucial part of maintaining a WordPress site, providing visibility into a part of the system that's often overlooked.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

Mercury rejected you. Here is the math behind it, and what to do next

The email arrives. "Mercury will not be able to support your business at this time. We will not be able to provide additional details about this decision." You spend the next two weeks building an…

  • Mercury rejects applications due to OFAC sanctions penalties up to $1 million per transaction
  • Rejection driven by reputational risk from media coverage on fintechs serving sanctioned Russians

More from Tuesday 1 September →