MySQL: Reclaiming disk space after data deletion

This documentation is part of the Troubleshooting guide. You can view the complete guide here: MySQL common issues and solutions.

👋 Welcome to the Stackhero documentation!

Stackhero offers a ready-to-use MySQL cloud solution that provides a host of benefits, including:

  • Unlimited connections and transfers.
  • phpMyAdmin web UI included.
  • Effortless updates with just a click.
  • Optimal performance and robust security powered by a private and dedicated VM.

Save time and simplify your life: it only takes 5 minutes to try Stackhero's MySQL cloud hosting solution!

When data is deleted from a MySQL table, the disk space it occupied is not immediately reclaimed. To recover this space, you can execute the following command:

OPTIMIZE TABLE <table>;

This process creates a temporary table, transfers the data to it, and then replaces the original table. During the optimisation process, the table will be locked and temporarily unavailable. The duration of this lock and the overall time required depend on the size of the table. Since this operation is resource-intensive, it is advisable to schedule it during periods of low server activity to minimise any impact on performance and availability.