Skip to main content

ExpireSnapshots

The ExpireSnapshots action removes old snapshots from a table and deletes data files that are no longer needed. This is essential for managing storage costs and maintaining table performance.

Interface

Overview

The ExpireSnapshots action is similar to the table API’s ExpireSnapshots operation but can leverage a query engine to distribute the deletion work. It safely removes:
  • Old snapshot metadata
  • Manifest files no longer referenced
  • Data and delete files orphaned by expired snapshots
  • Statistics files associated with expired snapshots
  • Optionally, unused partition specs and schemas

Methods

expireSnapshotId

Expires a specific snapshot identified by its ID.
Parameters:
  • snapshotId - The ID of the snapshot to expire
Returns: this for method chaining Example:

expireOlderThan

Expires all snapshots older than the given timestamp.
Parameters:
  • timestampMillis - A timestamp in milliseconds (from System.currentTimeMillis())
Returns: this for method chaining Example:

retainLast

Retains the most recent ancestors of the current snapshot, even if they would otherwise be expired.
Parameters:
  • numSnapshots - The number of recent snapshots to retain
Returns: this for method chaining Example:
Snapshots explicitly marked for expiration by ID will still be removed, even if they are among the most recent.

deleteWith

Provides a custom delete function for removing files.
Parameters:
  • deleteFunc - A function that accepts file paths to delete
Returns: this for method chaining Example:

executeDeleteWith

Provides an executor service for parallel file deletion.
Parameters:
  • executorService - The executor service to use for parallel deletes
Returns: this for method chaining
This is only used if a custom delete function is provided or if the FileIO doesn’t support bulk deletes.

cleanExpiredMetadata

Enables removal of unused table metadata like partition specs and schemas.
Parameters:
  • clean - true to remove unused metadata, false to keep it
Returns: this for method chaining Example:

Result

The Result interface provides statistics about the expiration operation.

Methods

Usage Examples

Basic Expiration

Retain Recent Snapshots

Expire Specific Snapshot

With Metadata Cleanup

Best Practices

Always use retainLast() to prevent accidentally expiring all snapshots, which would make time travel impossible.
  1. Set reasonable retention periods: Balance storage costs with the need for time travel
  2. Use retainLast() as a safety net: Always keep a minimum number of snapshots
  3. Monitor storage: Track the result metrics to understand storage savings
  4. Schedule regular expiration: Run this action periodically to prevent unbounded growth
  5. Test in development: Verify expiration behavior before running in production