Some source systems send a full snapshot for one slice of your data at a time: all products of one store, all orders of one day, all rows of one customer. You want the target table to take that slice as the new truth and leave every other slice alone. Databricks REPLACE USING flows do this in one statement, and they became generally available on December 1, 2026. This guide builds a store inventory table that reads snapshot files from a volume, tests what happens to a missing row, a late snapshot and a duplicate, and compares the result with a hand-written MERGE and with AUTO CDC. The complete code for this guide is on GitHub: databricks/replace-using.
What are REPLACE USING flows
A REPLACE USING flow reads a stream of partial snapshots and replaces the rows in the target that match the key columns of each incoming snapshot. It leaves the rest of the table unchanged.
- You name the key columns and one sequence column:
REPLACE USING (store_id) SEQUENCE BY snapshot_date. - For each key, the snapshot with the highest sequence wins, and an older snapshot is ignored.
- It works in Lakeflow pipelines, in SQL and Python, and for standalone streaming tables defined in SQL.
- It requires Databricks Runtime 18.2 and above, on classic or serverless compute.
- The release note, dated December 1, 2026, says REPLACE USING flows are generally available.
A snapshot for store S1 replaces the S1 rows. The other stores are not touched.
When to use it
Use a REPLACE USING flow when your source sends partial snapshots keyed by a column, and a new snapshot for a key should replace everything stored for that key. Typical cases are a daily file per store, a full export per customer, or a recalculated set of rows per partition. You do not need a primary key, because the flow works on the key group as a whole.
Use AUTO CDC when your source is a change feed with inserts, updates and deletes for single rows, or when you need history. Use MERGE when you need full control over each case, or when the target is a regular Delta table that you update in batches. The section "REPLACE USING, AUTO CDC or MERGE?" below shows the same scenario with all three.
Prerequisites
- A Unity Catalog workspace, and compute that runs Databricks Runtime 18.2 or above. A SQL warehouse or serverless compute works for standalone streaming tables. A pipeline works too.
- Permission to create a streaming table in a schema, and to create a volume.
- A source that is a streaming read. A batch source is not accepted.
Step-by-step guide
The steps use a store inventory scenario. Replace the placeholders in angle brackets with your own names.
Step 1: Create a volume for the snapshot files
Each file is a full snapshot for one store on one day, with the columns store_id, product_id, quantity and snapshot_date.
CREATE SCHEMA IF NOT EXISTS <catalog>.replace_demo; CREATE VOLUME IF NOT EXISTS <catalog>.replace_demo.snapshots; Upload the first three files (one per store, dated 2026-12-01) to /Volumes/<catalog>/replace_demo/snapshots/replace/.
Step 2: Create the streaming table with a REPLACE USING flow
The key is store_id and the sequence is snapshot_date. BY NAME is required, and the query must be a streaming read.
CREATE OR REFRESH STREAMING TABLE <catalog>.replace_demo.inventory FLOW REPLACE USING (store_id) SEQUENCE BY snapshot_date BY NAME SELECT store_id, product_id, CAST(quantity AS INT) AS quantity, CAST(snapshot_date AS DATE) AS snapshot_date FROM STREAM read_files('/Volumes/<catalog>/replace_demo/snapshots/replace/', format => 'csv', header => true); Step 3: Load a new snapshot and refresh
Upload the next file to the same folder and run the same statement again. The streaming table reads only the new files.
Step 4: The same flow in a pipeline
In a Lakeflow pipeline, the same flow reads from another streaming table. The SQL is the same. The Python version below uses the replace_using and sequence_by arguments (from the documentation; this guide tested the SQL version):
from pyspark import pipelines as dp @dp.table(name="inventory", replace_using=["store_id"], sequence_by="snapshot_date") def inventory(): return spark.readStream.table("<catalog>.replace_demo.inventory_raw") Standalone streaming tables support SQL only.
Example: store inventory snapshots
The scenario has four stores and five snapshot steps. Each table below shows the target after the step. The same steps ran on a standalone streaming table in SQL and in a pipeline, and the results were identical. The full example, including setup and cleanup, is in the replace-using folder of the repository.
Step 1: initial load, three stores. The target holds 15 rows: five products for each of S1, S2 and S3, all dated 2026-12-01.
Step 2: a new snapshot for S1, with changed quantities and product P5 missing.
| store | product | quantity | snapshot date |
|---|---|---|---|
| S1 | P1 | 38 | 2026-12-02 |
| S1 | P2 | 22 | 2026-12-02 |
| S1 | P3 | 55 | 2026-12-02 |
| S1 | P4 | 12 | 2026-12-02 |
| S2 | P1 to P5 | unchanged | 2026-12-01 |
| S3 | P1 to P5 | unchanged | 2026-12-01 |
The S1 quantities changed, and the other stores are untouched. S1's product P5 is gone: the row for P5 was deleted, because the flow replaces everything stored for the key S1 with the new snapshot. The target now has 14 rows.
Step 3: a late snapshot for S1, dated 2026-11-30. The target does not change. The snapshot's sequence is older than the stored sequence of 2026-12-02, so the flow ignores it, and the quantities of 99 in the late file never appear.
A snapshot with an older sequence than the stored one is ignored.
Step 4: two snapshots for S2 with the same date and different quantities. Both are appended, and S2 has 10 rows, two for each product. It makes no difference whether the two files arrive in the same refresh or in two refreshes.
Warning: equal sequence values for the same key keep both snapshots, which duplicates rows. This is the S2 result after step 4 for P1 and P2. P3 to P5 look the same:
| store | product | quantity | snapshot date |
|---|---|---|---|
| S2 | P1 | 31 | 2026-12-03 |
| S2 | P1 | 77 | 2026-12-03 |
| S2 | P2 | 46 | 2026-12-03 |
| S2 | P2 | 77 | 2026-12-03 |
Use a sequence column that is unique per key, such as a timestamp, or the date plus a file sequence number. SEQUENCE BY takes exactly one column, so combine the date and the number in one column. Both options worked in the test. With a unique sequence, the second snapshot replaced the first, and S2 kept 5 rows:
-- A timestamp that is unique per file SELECT store_id, product_id, quantity, snapshot_date, _metadata.file_modification_time AS snapshot_seq FROM STREAM read_files('<volume path>', format => 'csv', header => true) -- The date plus a file sequence number, here the letter at the end of the file name SELECT store_id, product_id, quantity, snapshot_date, CONCAT(CAST(snapshot_date AS STRING), '-', regexp_extract(_metadata.file_name, '_([a-z])[.]csv$', 1)) AS snapshot_seq FROM STREAM read_files('<volume path>', format => 'csv', header => true) Then use SEQUENCE BY snapshot_seq in the flow.
Step 5: a snapshot for a new store, S4. The three S4 rows are added. The target has 22 rows.
REPLACE USING, AUTO CDC or MERGE?
The same five steps ran with AUTO CDC in a pipeline (KEYS (store_id, product_id), SEQUENCE BY snapshot_date) and with MERGE. The results differ in how each approach handles a missing row, a late snapshot and a repeated sequence.
| Case | REPLACE USING | AUTO CDC | MERGE (complete) | MERGE (naive) |
|---|---|---|---|---|
| Missing row in a newer snapshot (S1 P5) | Deleted | Stays, with the old date | Deleted | Stays |
| Late, older snapshot | Ignored | Ignored | Ignored | Overwrites S1 with the old values |
| Same sequence twice | Both rows kept | One row per key | One row per key, first file kept | Last file wins |
| New store | Added | Added | Added | Added |
| Statement lines | 7 | 6, plus a 3 line staging table | 15, plus a store and a date for each run | 6 |
- AUTO CDC treats each row as a change for its key. It has no way to know that P5 is missing from the snapshot, so the old P5 row stays. AUTO CDC FROM SNAPSHOT treats keys absent from the snapshot as deleted, but it compares a full snapshot of the whole table, which is not what a per-store file is.
- A naive MERGE (match on store and product, update, insert) keeps the stale P5 row and lets the late snapshot overwrite newer data. It is the version most people write first.
- A complete MERGE needs a guard that skips a snapshot that is not newer, a delete branch for the rows that are missing, and the store and the date passed in for each run.
A common mistake: the delete condition in MERGE
When the guard filters a late snapshot out of the source, every row of its store has no match in the source. A delete branch that only checks the store then deletes all of them.
| Broken | Fixed | |
|---|---|---|
| Delete condition | WHEN NOT MATCHED BY SOURCE AND t.store_id IN ('S1') THEN DELETE | WHEN NOT MATCHED BY SOURCE AND t.store_id IN ('S1') AND t.snapshot_date < :snapshot_date THEN DELETE |
| When the late snapshot (2026-11-30) arrives | The guard removes the snapshot from the source, so every S1 row counts as missing and is deleted. S1 goes from 4 rows to 0 | :snapshot_date is 2026-11-30, and no S1 row is older than that, so nothing is deleted. S1 keeps its 4 rows |
The extra condition limits the delete to rows that are older than the snapshot. :snapshot_date is the incoming snapshot's date, passed in for each run, together with the store.
Use REPLACE USING when each snapshot is a full picture of one key. Use AUTO CDC when your source is a feed of row changes. Use MERGE when the rules are specific to your data.
Things to know
- Missing rows are removed. A new snapshot for a key replaces every row stored for that key, so a product that is missing from the snapshot is deleted. Each snapshot must hold every row for its key. The docs call them partial snapshots because each one covers only part of the table.
- Use a strictly increasing sequence. Rows with the same key and the same sequence are appended, which creates duplicates. Use a timestamp or a version that changes with every snapshot of a key.
- A string sequence column works, but strings sort alphabetically. A string sequence worked in the test. Use a format that sorts correctly, such as zero-padded dates (
2026-12-03, not2026-12-3) and single-letter suffixes (-a,-b). - Use a non-null sequence. The documentation says a null sequence can lead to undefined behavior.
- Exactly one SEQUENCE BY column and BY NAME. The flow takes one sequence column, and
BY NAMEis required. - Keys cannot be MAP or VARIANT. The query cannot contain aggregations, ORDER BY or window functions.
- One REPLACE USING flow per target. You cannot combine it with another flow type on the same table.
- Runtime 18.2 or above. Older runtimes do not run the flow.
- Standalone streaming tables are SQL only. Python works in pipelines.
- Full refresh starts over. For a standalone streaming table, a full refresh truncates the data, so keep the snapshot files in the volume.
Best practices
- Send complete snapshots for each key, and keep the key columns stable.
- Put the snapshot date or version in the file, and use it as the sequence.
- Do not rely on the order of files in a folder. The sequence decides.
- Check the table after the first late or duplicate snapshot in a test, before you rely on the behavior in production.
- Keep the snapshot files in the volume, so you can rebuild the table.
Summary
A REPLACE USING flow replaces the rows that match the key of each incoming snapshot, ignores snapshots that are older than the stored one, and leaves other keys alone. It replaces the whole key group, so a row missing from a newer snapshot is deleted, and a repeated sequence value creates duplicates. Use it when your source sends full snapshots for one slice at a time.
Which of your tables are reloaded one slice at a time today?


