MySQL
InnoDB
Disk Space
Data Deletion
Database Management

MySQL InnoDB not releasing disk space after deleting data rows from table

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

MySQL is a popular relational database management system, and it offers a variety of storage engines. Among them, InnoDB is the default storage engine for MySQL and is widely used due to its support for transactions, foreign keys, and row-level locking. However, a common issue faced by users pertains to InnoDB not releasing disk space after data deletion. This article explores the reasons why InnoDB behaves this way, technical underpinnings of its storage management, and possible solutions to reclaim disk space.

Understanding InnoDB Storage

InnoDB organizes data storage using a file known as the tablespace, which stores both the data and indexes. The tablespace can be a single or multiple files, and it expands as the data within the database grows. However, when data rows are deleted, the physical size of the tablespace does not decrease correspondingly. Here's why:

Why Doesn't InnoDB Release Disk Space Immediately?

  1. Fragmentation:
    • InnoDB does not automatically reclaim space through a garbage collection process analogous to some programming languages.
    • Deleting rows within a table results in fragmentation- where the space previously occupied by the deleted rows becomes available for new data within the same tablespace but isn't reclaimed at the OS level.
  2. Data "Free List":
    • InnoDB maintains a free list of pages in the tablespace file that are no longer holding active data.
    • When data is deleted, those pages are marked as free in the InnoDB internal data structures but remain part of the overall tablespace file size.
    • New data insertions can reuse these free pages, preventing further disk growth until these pages are exhausted.
  3. Extent Management:
    • InnoDB groups pages into extents.
    • Space can only be reclaimed if entire extents are freed, which doesn’t normally happen unless large blocks of contiguous data are deleted.

Examples and Illustration

Consider the following scenario:

  • Use the OPTIMIZE TABLE command:
  • This command rebuilds the table and reclaims unused space.
  • Export the table data using mysqldump :
  • Drop the table:
  • Restore the table:
  • This process creates a new tablespace file without the fragmentation.
  • Implementing table partitioning can be effective for managing large tables and making maintenance tasks like data cleanup more manageable.
  • Use the innodb_file_per_table option to store each table's data and index in a separate file, simplifying the process of space reclamation on a per-table basis.

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.