Course contentsShow
← Data Engineering
Lesson 284 of 8254. Modern Data Warehousing and LakehousesFree lesson

Managing Data Retention With Snapshot Expiration

Define and execute snapshot expiration policies to remove obsolete metadata and data files, effectively managing storage costs and compliance requirements.

In this lesson, you will learn how to automate the lifecycle of your table snapshots to balance historical auditability with storage efficiency.

The Challenge of Infinite History

When using modern table formats like Delta Lake or Apache Iceberg, every write operation creates a "snapshot." While these snapshots are incredible for time travel and disaster recovery, they consume storage space and metadata overhead indefinitely. If you have high-frequency writes, your storage costs will balloon, and your metadata layer will become bloated, potentially slowing down query planning.

What is Snapshot Expiration?

Snapshot expiration is the process of identifying snapshots that are older than your business or compliance requirements and removing them from the table’s metadata. By expiring an old snapshot, you are effectively telling the system: "I no longer need to travel back in time to this specific state."

Once a snapshot is expired, the system can safely identify the underlying data files that are no longer referenced by any valid snapshots and physically delete them, which directly reduces your cloud storage bill.

Implementation Pattern

In practice, you define a retention threshold (e.g., 30 days) and trigger an expiration policy. This is often handled through a maintenance command that reconciles the current state of the transaction log with your retention policy.

For example, in many modern lakehouse environments, the process looks like this:

-- Expire snapshots older than 30 days
-- This updates metadata and flags orphaned files for removal
CALL expire_snapshots('your_table_name', TIMESTAMP '2023-10-01 00:00:00');

-- Followed by a cleanup command to physically delete the files
VACUUM 'your_table_name' RETAIN 168 HOURS;

Why This Matters

If you are subject to GDPR or CCPA "right to be forgotten" requests, you cannot simply delete rows from the current table; you must also ensure that historical snapshots containing that data are expired and purged. Effective snapshot expiration keeps your data lake compliant while preventing "storage creep," where unused historical versions silently double or triple your infrastructure costs over time.

Key Takeaway: Snapshot expiration is a critical housekeeping task that purges obsolete metadata and unreferenced files to enforce compliance and minimize long-term storage expenditures.