The DELETE statement does not immediately release disk space — MySQL only marks records or data pages as reusable. To reclaim the tablespace and reduce disk usage, run the OPTIMIZE TABLE statement.
Prerequisites
-
The
OPTIMIZE TABLEstatement is supported only by the InnoDB and MyISAM storage engines. -
The available disk space of the instance must be greater than or equal to the size of the table that you want to optimize.
NoteIf the available disk space is insufficient, first expand the disk space. After the optimization completes, you can scale down as needed, and the system refunds the price difference.
Usage notes
-
Delete a large amount of data first:
OPTIMIZE TABLEdoes not reduce tablespace usage unless you first delete a significant amount of data with theDELETEstatement. -
Temporary increase in disk space usage:
OPTIMIZE TABLEcreates a temporary table for data reorganization, which temporarily increases disk usage. The temporary table is removed after the operation completes, and disk usage returns to normal. -
Table and index statistics may not change after reclamation: Although disk space is released, table statistics may not update immediately. For more information, see Why does the storage usage of my ApsaraDB RDS for MySQL instance remain unchanged after I run OPTIMIZE TABLE?.
-
Performance impact and risks during peak hours: On ApsaraDB RDS for MySQL 5.7 and 8.0,
OPTIMIZE TABLEuses online DDL, which allows concurrent DML operations. However, optimizing a large table can spike I/O and buffer usage, causing table locks or resource contention that may lead to instance unavailability or monitoring interruptions during peak hours. Perform this operation during off-peak hours.
Use the command line
-
Connect to the ApsaraDB RDS for MySQL instance by using a client.
-
Use the
DELETEstatement to delete unnecessary data based on your business requirements. -
Run the
OPTIMIZE TABLEstatement to reclaim the tablespace.OPTIMIZE TABLE <$Database1>.<Table1>,<$Database2>.<Table2>;Note-
<$Database1> and <$Database2> are database names. <Table1> and <Table2> are table names.
-
When you run the
OPTIMIZE TABLEstatement on the InnoDB storage engine, the following message is returned. This message is expected and can be ignored. The operation is successful if the output contains "ok". For more information, see OPTIMIZE TABLE Statement.Table does not support optimize, doing recreate + analyze instead
-
Use DMS
-
In the left-side navigation pane, select the instance ID of the target instance, double-click the target database, right-click any table name, and then select batch operation table.
-
Select the tables for which you want to reclaim space, and then choose .

-
In the dialog box that appears, confirm that the information is correct and click OK.