Sub topic 6
Why re-running COPY INTO is safe
This chapter assumes you can run the basic COPY INTO statement from the earlier chapters and know what a Delta table commit is. It explains how COPY INTO avoids loading a file twice, what that guarantee covers, and where it stops.
The guarantee in one sentence
COPY INTO is idempotent at the file level. If you run the same statement twice against the same source location and target table, files loaded by the first run are skipped by the second. The target table receives each file's rows once.
That property lets you schedule the statement every hour without writing any bookkeeping code. A run with no new files is cheap and changes nothing.
How file tracking works
COPY INTO keeps a record of the files it has loaded, and it keeps that record with the target table. Before reading anything, a run lists the files in the source location and compares each file against the record. Files already in the record are skipped. The remaining files are read and written to the table. The record is then updated as part of the same operation.
Several consequences follow from where the record lives.
- The record belongs to the target table. Loading the same folder into two different tables loads every file into both. Each table tracks its own history.
- The record is keyed by file identity, not by content. The check asks whether this file was already loaded. It does not ask whether these rows already exist in the table.
- Dropping and recreating the table resets the record. A new table has no history, so every file is loaded again.
- The source folder is not modified.
COPY INTOdoes not move, rename, or delete files, so the same folder can be listed repeatedly.
Seeing it happen
Start with a landing folder that holds two files, 2026-12-01.csv and 2026-12-02.csv. Run this statement.
COPY INTO main.sales.orders
FROM '/Volumes/main/sales/landing/orders/'
FILEFORMAT = CSV
FORMAT_OPTIONS ('header' = 'true');The statement returns one row of results. The columns num_affected_rows and num_inserted_rows count the rows written, and num_skipped_corrupt_files counts files skipped because they could not be read. For the first run, the row counts equal the total number of data rows in both files.
Now run the identical statement again without adding files. The result reports 0 rows, because both files are in the record. Add 2026-12-03.csv to the folder and run it a third time. Only the new file is read, and the row counts equal that file's rows.
You can confirm this from the table history. Each run that loads data is a commit, and a run that finds nothing new does not add rows.
DESCRIBE HISTORY main.sales.orders;Look at the operation and operationMetrics columns. The metrics show how many rows each load wrote. Compare them with the COPY INTO result and with SELECT COUNT(*) on the table to check that the three agree.
What idempotent covers, and what it does not
Idempotency here is a promise about files. It is not a promise about data. The table below lists cases where the two differ.
| Situation | What COPY INTO does | Why |
|---|---|---|
| Same file, second run | Skips it | The file is in the record |
| New file with new rows | Loads it | The file is not in the record |
| New file that repeats rows already loaded | Loads it, creating duplicate rows | The check does not compare row contents |
| An already-loaded file is edited in place | Normally does not reload it | The file is already recorded, so the new content is not picked up |
| Same folder loaded into a second table | Loads all files into that table | Each table has its own record |
| Table dropped and recreated | Loads everything again | The record went with the old table |
| `force` set to `true` | Loads all matching files again | Idempotency is switched off |
The third and fourth rows cause most surprises. A producer that re-exports yesterday's orders under a new file name will produce duplicates, even though your load is idempotent. A producer that overwrites orders.csv with corrected values will see the corrections ignored. The fix for both is in how files are delivered, not in the COPY INTO statement. Give every delivery a unique file name, and never edit files after they land.
Interaction with PATTERN and FILES
PATTERN and FILES decide which files are candidates. The record then decides which candidates are new. The two checks are independent.
COPY INTO main.sales.orders
FROM '/Volumes/main/sales/landing/orders/'
FILEFORMAT = CSV
PATTERN = '2026-12-*.csv'
FORMAT_OPTIONS ('header' = 'true');Suppose you run this statement today, then widen the pattern to '2026-*.csv' tomorrow. The files that matched yesterday are still in the record and are skipped. Only files that match the new, wider pattern and were not loaded before are read. Narrowing a pattern never removes rows that were loaded earlier.
Failed and partial runs
A load that fails midway does not leave the table half updated. The write is a Delta commit, so either the rows from the run are committed or they are not. A file whose rows were not committed is not recorded as loaded. Re-running the statement after a failure therefore picks up the files that did not make it, without duplicating the ones that did.
This is what makes retries safe in a job. If a cluster is lost or a task times out, you can re-run the same task as it is.
Overriding the guarantee on purpose
Sometimes you want the files loaded again. For example, you may have truncated the target table and want to rebuild it from the landing folder. The force copy option turns the record check off for that run.
COPY INTO main.sales.orders
FROM '/Volumes/main/sales/landing/orders/'
FILEFORMAT = CSV
FORMAT_OPTIONS ('header' = 'true')
COPY_OPTIONS ('force' = 'true');With force set to true, every matching file is loaded, including files loaded in earlier runs. If the table still holds their rows, those rows now appear twice. Use force only when the table is empty or when you have a plan for the duplicates. A later chapter covers reloading in detail.
Checking that a load is idempotent
You can test the guarantee on any table with three steps.
- Run the statement and note
SELECT COUNT(*)for the table. - Run the identical statement again.
- Count again. The two counts should be equal, and the second run should report 0 inserted rows.
If the count grows on the second run, check three things. First, confirm that force is not set. Second, confirm that the table was not recreated between runs. Third, confirm that the source files were not rewritten under new names.
Design rules that follow from this
- Give every delivered file a unique, never-reused name. A timestamp or batch ID in the name works well.
- Treat landed files as immutable. To correct data, deliver a new file and handle the correction in the table.
- Keep one target table per logical source folder, so the record reflects what you expect.
- Leave
forceoff by default. Turn it on for one-off rebuilds only. - Read the result row after every run and alert when
num_skipped_corrupt_filesis above 0.
Exercise
A nightly job runs the same COPY INTO statement against /Volumes/main/sales/landing/orders/. On Tuesday a producer overwrites 2026-12-01.csv, which loaded on Monday, with corrected amounts. On Wednesday a producer delivers 2026-12-01_v2.csv, which holds the same orders as the corrected file. What happens to the table after each delivery, and how would you avoid the problem?
Show answer
After the Tuesday overwrite, nothing changes. The file name is already in the table's record, so the nightly run skips it and the corrected amounts never load. After Wednesday's delivery, 2026-12-01_v2.csv is a new file, so it loads in full. Its orders are now duplicates of the Monday rows, because idempotency compares files and not row contents. To avoid both problems, treat landed files as immutable and never overwrite them. Deliver corrections as new files, and then apply them with a MERGE INTO keyed on the order ID, or load into a staging table and deduplicate before the final table.
Exercise
You run a COPY INTO statement with no force option and it reports 0 inserted rows, but you know new data arrived. List two causes to check.
Show answer
First, the new data may have been written into an existing file name that was already loaded, so the file is skipped. Second, the PATTERN or FILES clause may not match the new files, so they are never candidates. A third possibility is that the files were delivered to a different folder than the one in the FROM clause.