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
| Job | What it does | What it does not do |
|---|---|---|
| Compaction | Rewrites small data files and merges delete files into a new snapshot | Does not delete the old files, because older snapshots still point at them |
| Snapshot expiry | Removes old snapshots and the data files only they needed | Does not remove old metadata files unless you set the cleanup properties |
| Metadata file cleanup | Deletes the oldest tracked metadata files after each commit | Does not delete untracked, orphaned metadata files |
| Orphan file removal | Deletes files under the table location that no metadata references, usually left by failed jobs | Is 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.
-- 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.
-- 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.
-- 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
| Measure | Why it matters | Action when it rises |
|---|---|---|
| Data files per table | Many small files slow planning and scans | Run compaction on the affected partitions |
| Snapshots per table | Long history grows metadata | Expire snapshots to the retention you agreed |
| Table size against live data | Old files stay until snapshots expire | Check expiry ran after compaction |
| Metadata files in the table folder | Frequent commits leave many JSON files | Set the delete-after-commit properties |
| Query time on the top tables | The result you care about | Compare to the baseline before maintenance |
A weekly routine
- Monday: list tables by file count and snapshot count. The files metadata table in Iceberg shows file sizes.
- Compact the worst tables, partition by partition.
- After compaction, expire snapshots to the agreed retention.
- Record file counts and table size before and after, and the query time on the top five queries.
- Alert when a table crosses a file-count threshold.
- 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.




