How to Stop a Database Container From Growing After Data Cleanup

Eva Wong is the Technical Writer and resident tinkerer at ZimaSpace. A lifelong geek with a passion for homelabs and open-source software, she specializes in translating complex technical concepts into accessible, hands-on guides. Eva believes that self-hosting should be fun, not intimidating. Through her tutorials, she empowers the community to demystify hardware setups, from building their first NAS to mastering Docker containers.

A database container can keep growing after data cleanup because deleted rows, transaction logs, indexes, and container logs follow different space-reclamation rules.

Do not assume the database volume is growing just because the container’s total disk footprint rises. Measure the database data directory, WAL or binlog directories, Docker log file, writable layer, and backup or temporary paths separately. Then determine whether deleted database space is reusable internally but not returned to the host, whether a rewrite is actually required, or whether a completely different file is still growing.

Measure Which Path Is Still Growing

Record the size of the named volume or bind-mounted database directory, container writable layer, host-side container log, database transaction-log directory, and any dump or temporary directory before and after one cleanup cycle.

A PostgreSQL disk-space troubleshooting guide starts with the same principle: find where the space is before choosing a reclaim operation.

If only the Docker log grows, database cleanup is irrelevant. If the data file stays large but stops increasing after cleanup, the engine may already be reusing freed pages even though the host filesystem sees no shrink.

Distinguish Reusable Database Space From Returned Disk Space

Many transactional databases do not remove file blocks from the middle of a table immediately after rows are deleted. They mark or reclaim internal pages so later inserts can reuse that space while the underlying file remains the same size.

PostgreSQL provides a clear example: ordinary VACUUM reuses space internally but usually does not return those middle file regions to the operating system.

Watch whether the file continues growing during new inserts after cleanup. Stable file size with falling internal bloat is different from uncontrolled growth and usually does not justify an emergency rewrite.

Use the Database Engine’s Reclaim Method, Not a Generic Docker Cleanup

When the goal is to return capacity to the host, identify the engine and storage format first. PostgreSQL, MySQL or MariaDB, and SQLite do not use one interchangeable shrink command, and some reclaim operations rewrite large files or lock tables.

A MySQL storage article explains how OPTIMIZE can rebuild InnoDB tables rather than treating a DELETE as proof that the host should immediately regain bytes.

Back up the database and confirm free workspace before any rewrite-heavy operation. A nearly full home-server filesystem is the worst time to launch a command that needs a second copy of a large table.

-15% OFF
Single board computer zimaboard2

Check WAL, Binlogs, and Replication Retention Separately

Transaction logs can grow even after old application rows are deleted. A failed archive job, stale replication slot, lagging replica, long transaction, or retained backup requirement can keep historical log segments on disk.

A recent PostgreSQL recovery note shows how WAL retention can consume storage independently from the table data that a user just cleaned up.

Do not delete WAL or binlog files from the filesystem by hand. Correct the retention cause through the database engine, then verify that normal recycling resumes.

Cap Docker Logs and Check the Writable Layer

A database container can appear to grow because stdout or stderr is captured into an unbounded Docker log, or because a temporary export, cache, or database file was written into the container layer instead of the intended persistent volume.

A self-hosted Docker case notes that container logs can grow indefinitely when rotation is not configured.

Map each large host file back to its container path before deleting anything. Configure log rotation for future growth and move database state into an explicit volume rather than relying on the ephemeral writable layer.

Verify That Cleanup Creates Sustainable Headroom

After the chosen reclaim step, run the normal write workload for a representative period and compare host free space, database file size, transaction-log size, Docker logs, and internal free-space or bloat metrics.

A PostgreSQL storage-reduction guide emphasizes that shrinking requires targeted maintenance rather than assuming every delete should immediately reduce the operating-system file size.

The fix is complete when expected database growth is reused or bounded and the host no longer loses unexplained capacity after each cleanup cycle. The related ZimaSpace guide to consistent database-container backups provides the rollback boundary before any operation that rewrites database files.

Frequently Asked Questions

Why does deleting millions of rows sometimes free almost no host disk space?

The engine may mark those pages reusable inside the database file rather than truncating the file itself. That can prevent future growth without changing the host-visible file size.

Should I run a full rewrite every time the container gets large?

No. Rewrite-heavy operations can require locks, temporary space, and significant I/O. Use them only when returning space to the host is necessary and the engine-specific risks are understood.

Can Docker prune reclaim a database volume?

Not safely when the volume is still part of the database’s persistent state. Identify whether the space belongs to logs, images, stopped containers, or live database data before pruning.

Support & Tips

More to Read

Get More Builds Like This

Stay in the Loop

Get updates from Zima - new products, exclusive deals, and real builds from the community.

Stay in the Loop preferences

We respect your inbox. Unsubscribe anytime.