Maintenance operations require a loaded
Table instance. See the Java API quickstart for details on loading tables.Recommended Maintenance
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
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
Location-Based Cleanup
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 thefiles 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
- Many small manifests: After many small commits (streaming workloads)
- Misaligned write patterns: Write pattern doesn’t match read filters
- 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
- Automate maintenance: Schedule regular maintenance jobs
- Monitor table health: Track snapshot count, file sizes, and metadata growth
- Conservative expiration: Keep enough snapshots for typical time-travel needs
- Safe orphan cleanup: Use retention intervals longer than write job durations
- Compact incrementally: Target specific partitions to reduce processing time
- Test on copies: Validate maintenance procedures on table copies first
- Use Spark actions: Leverage parallel processing for large tables
- Enable auto-cleanup: Set
write.metadata.delete-after-commit.enabled=true