You deleted 80 million old rows from a log table to free disk space. The DELETE took an hour, and the disk is exactly as full as before. Or ibdata1 has grown to 120 GB and nothing makes it smaller.
InnoDB reuses freed space inside its files, but it doesn't hand it back to the operating system unless you rebuild. Here's where the space goes and how to reclaim it.
Where MySQL's disk space goes
-- Table data + indexes, and free space inside each table
SELECT table_schema, table_name,
ROUND((data_length + index_length) / 1024 / 1024 / 1024, 2) AS used_gb,
ROUND(data_free / 1024 / 1024 / 1024, 2) AS free_inside_gb
FROM information_schema.tables
WHERE engine = 'InnoDB'
ORDER BY (data_length + index_length + data_free) DESC
LIMIT 20;
Then look at the data directory itself (du -sh /var/lib/mysql/*) for the other big consumers:
<table>.ibdfiles, one per table (innodb_file_per_table=ON, the default since 5.6);ibdata1, the system tablespace;- binary logs (
binlog.000123…); - undo tablespaces (
undo_001,undo_002) and the temporary tablespace (ibtmp1); - slow and general query logs, if enabled.
Reclaim space from a table: rebuild it
After a big DELETE, the .ibd file keeps its size and the free pages are reused by future inserts. To shrink the file, rebuild the table:
OPTIMIZE TABLE app.logs;
-- For InnoDB this runs ALTER TABLE ... FORCE (recreate + analyze), online in most cases
- The rebuild needs free disk space roughly equal to the table's size while it runs. On a nearly full disk, rebuild the smaller tables first, or free space elsewhere.
- It's an online operation for most InnoDB tables (reads and writes continue), but it's I/O-heavy, so run it off-peak. For very large or busy tables, use
pt-online-schema-changeorgh-ostfor more control.
Better still, avoid big deletes. Partition log-style tables by date and drop old partitions, which is instant and frees the space immediately:
ALTER TABLE logs DROP PARTITION p2025_12;
ibdata1 never shrinks
If tables were created while innodb_file_per_table was off (old installs), their data lives in ibdata1. Moving them out is possible (ALTER TABLE t ENGINE=InnoDB with file-per-table on), but ibdata1 itself never gets smaller. The only way to shrink it is to dump and reload into a fresh instance:
mysqldumpormysqlpump/ MySQL Shell'sutil.dumpInstance()everything;- initialize a new, empty data directory with
innodb_file_per_table=ON; - load the dump, then switch over.
In MySQL 8, ibdata1 growth usually comes from undo logs of long-running transactions on older setups. Undo now lives in separate undo tablespaces that are truncated automatically (innodb_undo_log_truncate=ON), as long as no long transaction holds old undo.
Binary logs
Binlogs are often the real space hog on busy servers:
SHOW BINARY LOGS;
SHOW VARIABLES LIKE 'binlog_expire_logs_seconds'; -- default 2592000 = 30 days in 8.0
Lower the retention to what your replicas and point-in-time recovery actually need:
SET PERSIST binlog_expire_logs_seconds = 604800; -- 7 days
PURGE BINARY LOGS BEFORE NOW() - INTERVAL 7 DAY;
Never delete binlog files with rm. That corrupts the binlog index. And make sure no replica still needs those files (SHOW REPLICA STATUS on each replica) before purging.
Temporary space
ibtmp1 (internal temporary tables from big sorts and GROUP BY) can grow very large during heavy queries and only shrinks on restart. Cap it with innodb_temp_data_file_path = ibtmp1:12M:autoextend:max:20G, and fix the queries creating huge temporary tables.
Checklist
DELETEfrees space inside the.ibdfile;OPTIMIZE TABLEgives it back (with enough free disk for a copy).- Partition by date and drop partitions instead of mass deletes.
ibdata1only shrinks by dump and reload into a fresh instance.- Set binlog retention and
PURGE BINARY LOGS, neverrm, and capibtmp1.
Get the weekly commit
New database deep dives every week.
