Home / Data Platform / Lakehouse

Data PlatformCheat sheetIntermediate

Delta Lake change data feed cheat sheet: enable it, read changes by version or timestamp, and the errors it throws

Commands to enable Delta change data feed, read row-level changes in SQL, PySpark and streaming, get the latest row per key, and fix the missing-change-data and out-of-range errors.

Deepak Bhardwaj6 min read

Applies toDelta Lake 2.x-4.x (open source), Databricks Runtime and Azure Databricks; error class names vary by runtime

Change data feed (CDF) records row-level inserts, updates and deletes for a Delta table so downstream jobs can read only what changed instead of rescanning the whole table. Most on-call tickets about it are one of three things: CDF was never enabled for the versions being read, the change files were removed by retention or VACUUM, or the consumer treats update_preimage rows as real data. This page has the commands for each.

Enable and check change data feed

CDF only records changes from the commit where it was enabled. Nothing before that version can be read as changes.

-- Existing table
ALTER TABLE sales.orders SET TBLPROPERTIES (delta.enableChangeDataFeed = true);

-- New table
CREATE TABLE sales.orders_v2 (order_id BIGINT, status STRING, amount DECIMAL(18,2))
TBLPROPERTIES (delta.enableChangeDataFeed = true);

-- Confirm the property
SHOW TBLPROPERTIES sales.orders;

To turn it on for every new table a session creates:

SET spark.databricks.delta.properties.defaults.enableChangeDataFeed = true;

Find the version where CDF was switched on. Look for the SET TBLPROPERTIES operation in the history:

DESCRIBE HISTORY sales.orders;
from pyspark.sql import functions as F

(spark.sql("DESCRIBE HISTORY sales.orders")
   .filter(F.col("operation") == "SET TBLPROPERTIES")
   .select("version", "timestamp", "operationParameters")
   .show(truncate=False))

Warning

Enabling CDF fails if the table already has columns named _change_type, _commit_version or _commit_timestamp. These names are reserved for the CDF metadata columns, so rename the columns first.

Read changes in batch

SQL

-- By version range (inclusive)
SELECT * FROM table_changes('sales.orders', 12, 20);

-- From a version to the latest
SELECT * FROM table_changes('sales.orders', 12);

-- By timestamp (string in a format Spark can cast to a timestamp)
SELECT * FROM table_changes('sales.orders', '2026-11-29 00:00:00', '2026-11-30 00:00:00');

PySpark

changes = (spark.read.format("delta")
    .option("readChangeFeed", "true")
    .option("startingVersion", 12)
    .option("endingVersion", 20)      # optional
    .table("sales.orders"))

# Or by time
changes = (spark.read.format("delta")
    .option("readChangeFeed", "true")
    .option("startingTimestamp", "2026-11-29 00:00:00")
    .table("sales.orders"))

The metadata columns

Each change row carries three extra columns:

Column Values What to do with it
_change_type insert, update_preimage, update_postimage, delete Drop update_preimage unless you need the old value for auditing
_commit_version Delta table version of the commit Use it to order changes and as your restart bookmark
_commit_timestamp Commit time Use it for reporting, not ordering. Use the version for ordering

Count what happened per commit. This is a quick way to check whether a job wrote what you expected:

SELECT _commit_version, _change_type, count(*) AS row_count
FROM table_changes('sales.orders', 12)
GROUP BY _commit_version, _change_type
ORDER BY _commit_version, _change_type;

Latest state per key

A key can change several times in one range. If you apply every row in order you will update the target more than once, and a MERGE will fail with "multiple source rows matched". Keep only the last change for each key:

from pyspark.sql import Window, functions as F

w = Window.partitionBy("order_id").orderBy(F.col("_commit_version").desc())

latest = (changes
    .filter(F.col("_change_type") != "update_preimage")
    .withColumn("rn", F.row_number().over(w))
    .filter("rn = 1")
    .drop("rn"))

Then merge it into the target. Deletes become deletes, and everything else becomes an upsert:

from delta.tables import DeltaTable

target = DeltaTable.forName(spark, "reporting.orders_current")
(target.alias("t")
   .merge(latest.alias("s"), "t.order_id = s.order_id")
   .whenMatchedDelete(condition="s._change_type = 'delete'")
   .whenMatchedUpdate(condition="s._change_type != 'delete'",
                      set={"status": "s.status", "amount": "s.amount"})
   .whenNotMatchedInsert(condition="s._change_type != 'delete'",
                         values={"order_id": "s.order_id", "status": "s.status", "amount": "s.amount"})
   .execute())

Store the highest _commit_version you processed, and start the next run from that value plus one.

Read changes as a stream

stream = (spark.readStream.format("delta")
    .option("readChangeFeed", "true")
    .option("startingVersion", 12)    # only used on the first run; the checkpoint wins after that
    .table("sales.orders"))

(stream.writeStream
    .option("checkpointLocation", "/Volumes/ops/chk/orders_cdf")
    .trigger(availableNow=True)
    .toTable("audit.orders_changes"))

Tip

startingVersion and startingTimestamp are ignored once the checkpoint exists. To start a stream from a different version, use a new checkpoint location.

Errors and fixes

Symptom Cause Fix
Error getting change data for range [x, y] as change data was not recorded for version [x] (DELTA_MISSING_CHANGE_DATA on Databricks) The start version is before CDF was enabled, or the change files for it were removed Find the enable version in DESCRIBE HISTORY and start after it. Fill older history with a snapshot read or time travel instead
Timestamp is after the latest commit (DELTA_TIMESTAMP_GREATER_THAN_COMMIT or similar) A scheduled job asks for "since last run" and nothing has been committed since Handle "no changes" in the job, or set spark.databricks.delta.changeDataFeed.timestampOutOfRange.enabled = true so out-of-range starts return an empty result. Some serverless compute does not allow this setting
Version or file not found for an old range Log retention or VACUUM removed the change files Read the table as a full snapshot and rebuild the target, then run the consumer more often than the retention window
Duplicate updates in the target update_preimage rows were applied, or several changes per key were applied Filter out pre-images and keep the latest row per key (see above)
Batch CDF read fails across a column rename or drop The range contains a non-additive schema change on a column-mapping table Read the ranges before and after the change separately. Behaviour varies by version, so check the docs for your runtime
Change read is empty after a big load The job wrote to a different table, or CDF was enabled after the load Check DESCRIBE HISTORY for the version and operation

Retention and VACUUM

Change files are stored in the table's _change_data folder. They follow the same retention rules as other data files:

  • VACUUM deletes change files that are older than the retention threshold, just like regular data files.
  • History is kept according to delta.logRetentionDuration, and removed files according to delta.deletedFileRetentionDuration.
SHOW TBLPROPERTIES sales.orders;

ALTER TABLE sales.orders SET TBLPROPERTIES (
  delta.logRetentionDuration = 'interval 30 days',
  delta.deletedFileRetentionDuration = 'interval 7 days'
);

Caution

Running VACUUM ... RETAIN 0 HOURS on a CDF table deletes change data that consumers have not read yet. Those changes cannot be recovered from CDF afterwards. The only way back is a full rebuild from the current snapshot.

Prevent it next time

  • Enable CDF when you create the table, not after the first consumer asks for it.
  • Store the last processed _commit_version per consumer, and alert if a consumer falls further behind than delta.deletedFileRetentionDuration.
  • Always filter out update_preimage and deduplicate by key before a MERGE.
  • Treat "no new commits" as a normal result in scheduled jobs.
  • Check DESCRIBE HISTORY before running VACUUM with a shorter retention on any table that has CDF consumers.