Duczer East
Healthcare

In-perimeter AI on governed clinical and operational data.

Financial Services

Private AI and data platforms built to clear model-risk and audit review.

Financial services

Private AI, KYC/AML, API gateways and data governance for banks.

Insurance

Change data capture, governed agents and integration for insurers.

Healthcare

PHI document intelligence, FHIR APIs and in-perimeter AI for health systems and plans.

GuideCloudera / Data Services & AI8 min read

Iceberg table maintenance on Cloudera: compaction, snapshot expiry and monitoring

A weekly routine that keeps query times and storage under control

Authored byCo-Founder and Managing Partner, Duczer East
Part of the Insights topic: Private AI & Data Architecture
On this page
  1. Why Iceberg tables degrade
  2. The four jobs and what each one frees
  3. A compaction schedule
  4. If you compact with Spark
  5. Snapshot expiry
  6. Metadata files and orphan files
  7. What to monitor
  8. A weekly routine
  9. Questions we hear

Iceberg tables on Cloudera stay fast when you do four things on a schedule: compact small files, expire old snapshots, clean up metadata files and watch file counts. Compaction rewrites data files, so it takes time and temporarily adds files. Snapshot expiry then lets storage fall. This runbook gives the commands, the order and a weekly routine.

Why Iceberg tables degrade

Each write creates a snapshot, and streaming or frequent micro-batches create many small data files. Row-level changes add delete files too. A query has to plan across all of them. The Apache Iceberg documentation puts it plainly: more data files means more metadata in manifest files, and small files add file-open cost to every query. The table is not broken: it just carries more files and metadata than it needs. The fix is routine maintenance.

The four jobs and what each one frees

JobWhat it doesWhat it does not do
CompactionRewrites small data files and merges delete files into a new snapshotDoes not delete the old files, because older snapshots still point at them
Snapshot expiryRemoves old snapshots and the data files only they neededDoes not remove old metadata files unless you set the cleanup properties
Metadata file cleanupDeletes the oldest tracked metadata files after each commitDoes not delete untracked, orphaned metadata files
Orphan file removalDeletes files under the table location that no metadata references, usually left by failed jobsIs not safe with a short retention interval while writes are running

The order matters. Compact first, then expire snapshots, because expiry is what turns the rewritten files into free space.

A compaction schedule

Hive and Impala can both compact Iceberg tables, and Cloudera notes that compaction of the whole table can take a long time. Without a threshold the entire table is rewritten, so use a threshold for recurring jobs and compact the partitions that change, not the whole table. Run it outside peak query hours.

In Impala, the OPTIMIZE TABLE statement rewrites small files. The FILE_SIZE_THRESHOLD_MB option sets the maximum size of data files considered, and larger files are rewritten only if delete files reference them. Hive has its own syntax.

sql
-- Impala: compact small files in an Iceberg table
OPTIMIZE TABLE sales.orders;

-- Only rewrite files below a size threshold (MB)
OPTIMIZE TABLE sales.orders (FILE_SIZE_THRESHOLD_MB=128);

-- Hive: major compaction, or a rewrite
ALTER TABLE sales.orders COMPACT 'MAJOR';
OPTIMIZE TABLE sales.orders REWRITE DATA;

Cloudera lists these conditions for OPTIMIZE TABLE:

  • The user needs ALL permission on the table, which you can set through Ranger.
  • Impala writes only Parquet, so the write.format.default table property must be parquet. Hive can write Parquet and ORC.
  • Impala cannot compact tables with complex data types, and it cannot compact views.
  • With the threshold, only files that meet the size criteria move to the latest schema and partition spec. Run it without the threshold now and then to rewrite the whole table.

If Cloudera Data Warehouse and Cloudera Data Hub share one Hive Metastore, Cloudera warns that Data Hub compaction workers can pick up Iceberg compaction tasks and fail. Run Iceberg compaction in Cloudera Data Warehouse, or set hive.compactor.worker.threads to 0 on HiveServer2 in Data Hub. Check the statement and options against your Runtime release.

If you compact with Spark

Apache Iceberg's rewrite_data_files procedure is the Spark route. By default it uses the binpack strategy, targets files of 512 MB, rewrites file groups that hold at least 5 files, and treats files under 75 percent of the target as too small. A where clause limits it to the partitions that changed.

sql
-- Spark: compact one day of data
CALL catalog_name.system.rewrite_data_files(
  table => 'sales.orders',
  where => 'order_date = "2026-11-23"');

Those defaults come from the Iceberg documentation, so confirm the Iceberg version that ships with your Runtime before relying on them.

Snapshot expiry

Expire snapshots older than a date you choose. Hive and Impala both support this. Keep enough history for time travel and rollback, and use the history.expire.min-snapshots-to-keep table property to set a floor. Cloudera points out that this property counts snapshots, not time: with hourly writes a value of 24 keeps about a day, but a table that commits every minute needs a much larger number.

sql
-- Impala: expire snapshots older than a timestamp
ALTER TABLE sales.orders EXECUTE expire_snapshots('2026-11-01 00:00:00');

-- Impala: expire by an interval
ALTER TABLE sales.orders EXECUTE expire_snapshots(now() - interval 10 days);

-- Hive: the same idea
ALTER TABLE sales.orders EXECUTE EXPIRE_SNAPSHOTS(CURRENT_TIMESTAMP - interval 10 days);

-- Keep at least five snapshots whatever their age
ALTER TABLE sales.orders SET TBLPROPERTIES ('history.expire.min-snapshots-to-keep'='5');

Hive also accepts a single snapshot ID, a list of IDs, or a range between two timestamps. Cloudera notes that removing specific snapshots is one way to meet a right-to-erasure request.

Expiry removes the ability to roll back to the expired snapshots. Agree the retention with the owners of each table first. In the Iceberg Spark procedure the defaults are snapshots older than 5 days, with at least the latest snapshot kept, which is far shorter than many audit rules need.

Metadata files and orphan files

Expiring a snapshot does not remove old metadata files by default. Set write.metadata.delete-after-commit.enabled to true and cap write.metadata.previous-versions-max, which defaults to 100, so the oldest tracked file is deleted after each commit. This matters most on streaming tables with very frequent commits. It only deletes files the metadata log still tracks.

Orphan files come from failed or cancelled jobs. The Iceberg documentation says to run orphan file removal now and then, not often, and warns that a retention interval shorter than the longest write is dangerous, because in-progress files can look orphaned. The default interval is 3 days. It also warns of data loss if paths change over time, for example after an HDFS authority change, so check paths first.

What to monitor

MeasureWhy it mattersAction when it rises
Data files per tableMany small files slow planning and scansRun compaction on the affected partitions
Snapshots per tableLong history grows metadataExpire snapshots to the retention you agreed
Table size against live dataOld files stay until snapshots expireCheck expiry ran after compaction
Metadata files in the table folderFrequent commits leave many JSON filesSet the delete-after-commit properties
Query time on the top tablesThe result you care aboutCompare to the baseline before maintenance

A weekly routine

  1. Monday: list tables by file count and snapshot count. The files metadata table in Iceberg shows file sizes.
  2. Compact the worst tables, partition by partition.
  3. After compaction, expire snapshots to the agreed retention.
  4. Record file counts and table size before and after, and the query time on the top five queries.
  5. Alert when a table crosses a file-count threshold.
  6. Once a quarter, run orphan file removal with the default retention and review what it found.

Questions we hear

Why did the table get bigger after compaction? The rewritten files are new, and the old ones stay until the snapshots that reference them expire.

Can we compact while users query? Compaction creates a new snapshot, so readers keep working on the old one. It still uses cluster resources, so schedule it off-peak.

Moving Hive tables to Iceberg first? See Hive to Iceberg migration steps. For the worked example see an Iceberg maintenance programme, and for sharing these tables with other engines see the Iceberg REST catalog guide.

Frequently asked questions

Why do Iceberg tables slow down over time?

Frequent small writes create many small data files and many snapshots. Queries then open more files and read more metadata. Compaction merges small files and snapshot expiry removes old metadata.

Can Hive and Impala compact Iceberg tables on Cloudera?

Yes. Cloudera documents compaction of Iceberg tables from both Hive and Impala, and Impala has an OPTIMIZE TABLE statement.

Does compaction free storage straight away?

No. Compaction writes new files first. The old files only go when you expire the snapshots that reference them, so file counts can rise before they fall.

How often should we compact?

Start weekly and adjust by watching file counts. Tables with heavy streaming writes need it more often.

Does compaction free storage straight away?

No. Compaction writes a new snapshot and leaves the old files, because older snapshots still reference them. Expire snapshots afterward to release the space.

What are the default snapshot expiry settings in Apache Iceberg?

The Spark expire_snapshots procedure defaults to snapshots older than 5 days and keeps at least the latest snapshot. Table properties can set a minimum number of snapshots to keep.

Is it safe to remove orphan files?

Only with a retention interval longer than your longest write. The Iceberg default is 3 days, and a shorter value can delete files from jobs still running.

Authored by

Co-Founder and Managing Partner, Duczer East

David deBoisblanc is co-founder of Duczer East and a digital transformation and innovation leader in data-centric AI, agentic systems, and enterprise integration. He shapes intent-driven AI solutions that modernize how organizations use data, drawing on deep experience with Cloudera and complex enterprise integration landscapes.

Newsletter

The Practitioner's Briefing

Senior-level insights on agentic AI, data engineering and enterprise integration, delivered to your inbox.

One email when there is something worth reading. Unsubscribe any time.

Let's Talk

Want to discuss the ideas raised here?

Tell us what you are working on. A senior practitioner replies, and only your email is required.

Get a quote