Skip to main content
Regular maintenance is essential for optimal Iceberg table performance. This guide covers recommended and optional maintenance operations to keep your tables healthy.
Maintenance operations require a loaded Table instance. See the Java API quickstart for details on loading tables.
These operations should be performed regularly on all production tables.

Expire Snapshots

Each write to an Iceberg table creates a new snapshot (version) of the table. Snapshots enable time-travel queries and rollback, but they accumulate over time and must be expired to delete unused data files and keep metadata compact.
1

Basic expiration

Expire snapshots older than a specific timestamp:
2

Retain recent snapshots

Keep a minimum number of recent snapshots even if they’re older than the expiration time:
3

Expire specific snapshots

Expire a specific snapshot by ID:

Parallel Expiration with Spark Actions

For large tables, use Spark actions to expire snapshots in parallel:

Cleanup Levels

Control what gets cleaned up during expiration:
Use METADATA_ONLY when data files are shared across tables or when using procedures like add-files that may reference the same data files.

Custom Deletion and Parallelism

Data files are not deleted until they are no longer referenced by any snapshot that may be used for time travel or rollback. Regularly expiring snapshots is essential to reclaim storage.

Remove Old Metadata Files

Iceberg tracks table metadata using JSON files. Each table change produces a new metadata file for atomicity. Over time, these accumulate and need cleanup.

Automatic Metadata Deletion

Configure automatic deletion of old metadata files:

Metadata Cleanup Examples

Orphaned metadata files (untracked in the metadata log) are only cleaned by the orphan file deletion procedure.

Delete Orphan Files

Task or job failures in distributed processing can leave files unreferenced by table metadata. The orphan file deletion action cleans these up.

Configure Retention Interval

It is dangerous to remove orphan files with a retention interval shorter than the time expected for any write to complete. The default interval is 3 days. Setting it too short might corrupt the table by deleting in-progress files.

Location-Based Cleanup

Iceberg uses string representations of paths when determining which files to remove. On some file systems, paths can change over time while representing the same file (e.g., HDFS authority changes). This will lead to data loss when orphan file deletion is run. Ensure entries in metadata tables match current file listings.

Optional Maintenance

These operations improve performance for specific workload patterns.

Compact Data Files

Small data files increase metadata overhead and reduce query performance. The rewrite data files action combines small files into larger ones.

When to Compact

Use the files metadata table to identify partitions needing compaction:

Compaction Options

Streaming queries that produce many small files are prime candidates for regular compaction.

Rewrite Manifests

Manifests organize file metadata for efficient query planning. Rewriting manifests can optimize metadata access patterns.

Basic Manifest Rewrite

API-Based Manifest Rewrite

When to Rewrite Manifests

  1. Many small manifests: After many small commits (streaming workloads)
  2. Misaligned write patterns: Write pattern doesn’t match read filters
  3. After large deletes: After deleting significant amounts of data
Manifests are automatically compacted in write order. Rewriting is most beneficial when the write pattern doesn’t align with query patterns.

Maintenance Schedule

Recommended maintenance schedule for production tables:

Complete Maintenance Example

Best Practices

  1. Automate maintenance: Schedule regular maintenance jobs
  2. Monitor table health: Track snapshot count, file sizes, and metadata growth
  3. Conservative expiration: Keep enough snapshots for typical time-travel needs
  4. Safe orphan cleanup: Use retention intervals longer than write job durations
  5. Compact incrementally: Target specific partitions to reduce processing time
  6. Test on copies: Validate maintenance procedures on table copies first
  7. Use Spark actions: Leverage parallel processing for large tables
  8. Enable auto-cleanup: Set write.metadata.delete-after-commit.enabled=true