1. The original problem
A small data pipeline stored its results in a DuckDB database file. The data inside was modest, about 7.4 million rows, but the file on disk was 19 GB and got bigger after every daily run.
When all the data was copied into a brand-new file, that file was only 1.7 GB. About 90% of the original file was wasted space.
2. Is it the application or DuckDB?
Both, but the trigger is how the application updates data.
- The application’s part: every day it updated data by deleting a group of rows and inserting new ones (for example, “delete all rows for these 15 customers, then insert their recalculated rows”). It did this in many small batches, day after day.
- DuckDB’s part: DuckDB is built for reading and analysing large tables. It stores data in large compressed blocks. This means:
- A
DELETEonly marks rows as deleted. The space is not given back to the disk. - Many small
INSERTs create many small, half-empty blocks. - The file never shrinks on its own, even after a
CHECKPOINT.
- A
So this is not a bug in DuckDB. It is how DuckDB is designed. A “delete and reinsert in small batches” pattern is close to the worst case for it.
In short: frequent small deletes and inserts + a file format that does not shrink = a file that keeps growing.
3. Options to resolve it
| Option | What it means | Does it help? |
|---|---|---|
Run CHECKPOINT | Ask DuckDB to tidy up after writes | No. Freed space is kept for reuse inside the file; the file stays the same size. It was already being done. |
| Copy into a fresh file | Write all live data into a new DuckDB file and swap it in | Yes. The new file has no deleted rows and no half-empty blocks. |
| Change the update pattern | Write fewer, larger batches, or avoid repeated delete-and-reinsert | Yes, long term. It reduces how fast the waste builds up. |
| Store less churn-heavy data in DuckDB | Don’t keep data that is rewritten every day (such as pre-built pages or JSON) in DuckDB | Yes. The largest table was exactly that kind of data. |
| Remove unneeded indexes | Indexes on long text columns took a lot of space | Partly. It saves space, but does not stop the growth. |
| Move the data to another database | Use a database built for frequent updates (such as MariaDB/MySQL or PostgreSQL) | Yes, if the data is updated often. |
4. The solution used
Copy the data into a fresh file, check it, then swap the files.
import duckdb
con = duckdb.connect() # temporary in-memory connection
con.execute("ATTACH 'analytics.duckdb' AS src (READ_ONLY)") # old file, read-only so it cannot be harmed
con.execute("ATTACH 'analytics.compact.duckdb' AS dst") # new, empty file
con.execute("COPY FROM DATABASE src TO dst") # copies tables, views, sequences, indexes
con.execute("CHECKPOINT dst")Before swapping:
- Check the data is identical. For every table, compare the row count and a hash of all rows in the old and new file:
SELECT count(*), sum(hash(t)) FROM src.my_table t; SELECT count(*), sum(hash(t)) FROM dst.my_table t; - Swap the files by renaming them (instant on the same drive).
- Keep the old file until the next normal run succeeds, then delete it.
Result:
| File | Before | After | Copy time |
|---|---|---|---|
| Main file | 19.15 GB | 1.69 GB | 22 s |
| Second file | 3.80 GB | 2.63 GB | 43 s |
| Total | 22.95 GB | 4.32 GB |
Important: this is a reset, not a permanent fix. After the very next daily run, which updated only 15 records, the main file grew from 1.69 GB to 2.22 GB. The waste comes back as long as the update pattern stays the same.
What happened next: the data that was rewritten most often was later moved out of DuckDB into MariaDB, a database designed for frequent updates. That removed the main cause.
5. If you use DuckDB regularly: how to handle this
Understand what DuckDB is good at. It is excellent for loading data in bulk and analysing it. It is not designed for constant small updates and deletes.
Habits that prevent the problem:
- Prefer bulk writes. Rebuild a whole table in one go (
CREATE OR REPLACE TABLE ... AS SELECT ...) instead of many small delete-and-insert steps. - Batch your changes. One large insert is much better than hundreds of small ones.
- Keep frequently changing data elsewhere. If rows are updated or deleted every day, keep them in a transactional database (PostgreSQL, MySQL/MariaDB, SQLite) and use DuckDB to analyse them.
- Only create indexes you actually need. DuckDB is usually fast without them, and they take space.
- Compact on a schedule. If you must update in place, copy the database into a fresh file regularly (after each run, or weekly), using the steps in section 4.
How to check whether your file is bloated:
-- 1. How many blocks the file uses (each block is 256 KB)
PRAGMA database_size;
-- 2. How many blocks each table really uses
SELECT count(DISTINCT block_id) AS blocks
FROM pragma_storage_info('my_table')
WHERE persistent;The quickest test: copy the database into a scratch file with COPY FROM DATABASE. If the copy is much smaller than the original, the difference is wasted space you can get back.