Sub topic 11
Handling files that already exist
This chapter assumes you can read a basic Auto Loader stream, that you know what a checkpoint is, and that you have seen the availableNow trigger. The question here is what happens on the first run, when the landing folder already holds thousands of files that arrived before the stream existed.
What the first run does
A new Auto Loader stream has an empty checkpoint, so it knows about no files. By default it treats every file already in the input path as new. The first run is therefore a backfill, followed by normal incremental ingestion.
The option that controls this is cloudFiles.includeExistingFiles. It defaults to true.
| Value | First run behavior | Typical use |
|---|---|---|
| `true` (default) | Processes all files already in the path, then files that arrive later | Loading history and keeping up with new data |
| `false` | Ignores files present at the first start, processes only files that arrive afterwards | History is already loaded some other way |
The option is read only when the stream starts with an empty checkpoint. After the checkpoint exists, changing the value has no effect. The checkpoint already records what the stream has seen, and it overrides the setting.
Running a backfill
A backfill can involve millions of files, so you should control how much each micro-batch reads. The rate limits from the previous chapter apply to the backfill as well. If you leave them unset, the default is 1000 files per micro-batch.
The next example loads all existing files and then stops, in as many batches as it needs.
df = (spark.readStream
.format("cloudFiles")
.option("cloudFiles.format", "json")
.option("cloudFiles.schemaLocation", "/Volumes/main/raw/_schemas/orders")
.option("cloudFiles.includeExistingFiles", "true")
.option("cloudFiles.maxFilesPerTrigger", "5000")
.load("/Volumes/main/raw/landing/orders"))
(df.writeStream
.option("checkpointLocation", "/Volumes/main/raw/_checkpoints/orders")
.trigger(availableNow=True)
.toTable("main.bronze.orders"))With availableNow=True, Spark splits the pending files into batches that respect maxFilesPerTrigger. It processes them one after another and then stops. Each batch commits to the checkpoint separately. If the job fails after batch 40 of 100, the next run starts at batch 41 instead of at the beginning.
Two practical points apply to large backfills:
- Directory listing on the first run has to enumerate every file under the path. That can take a long time on a path with millions of objects. The cost is paid once. Later runs list only what is new.
- A single huge first run can be hard to schedule. If the history is large, run the backfill on a bigger cluster, then move the same stream to a smaller one for steady-state ingestion. The checkpoint stays valid when the cluster changes.
Skipping history
Sometimes the history is already in the target table, or you do not want it. Setting includeExistingFiles to false skips it.
df = (spark.readStream
.format("cloudFiles")
.option("cloudFiles.format", "json")
.option("cloudFiles.schemaLocation", "/Volumes/main/raw/_schemas/orders")
.option("cloudFiles.includeExistingFiles", "false")
.load("/Volumes/main/raw/landing/orders"))Remember that this applies only to the first start. If you create the stream, run it once with the default, and later change the option to false, nothing changes. The history was already ingested.
Skipping history has a gap risk. Files that land between your last manual load and the first start of the stream can be missed. To close that gap, either stop writers while you cut over, or use a time filter instead of includeExistingFiles=false.
Selecting files by modification time
The generic file source options modifiedAfter and modifiedBefore filter files by their modification timestamp. They let you backfill a window of history, or exclude old files, without relying on first-start behavior.
df = (spark.readStream
.format("cloudFiles")
.option("cloudFiles.format", "json")
.option("cloudFiles.schemaLocation", "/Volumes/main/raw/_schemas/orders")
.option("modifiedAfter", "2026-01-01 00:00:00.000000 UTC+0")
.load("/Volumes/main/raw/landing/orders"))This stream ingests only files modified on or after 1 January 2026, whether they exist now or arrive later. The timestamp format is YYYY-MM-DD HH:mm:ss.SSSSSS followed by a UTC offset. Use pathGlobFilter when you need to select by file name or folder pattern instead, for example *.json.
Exactly once, and what it does not cover
The checkpoint records each discovered file, so a file is ingested once. This holds across restarts and failures. Two situations catch people out.
Files that change after ingestion
By default, Auto Loader processes a file once, even if someone overwrites it later with different content. The checkpoint knows the path and does not look again. If your upstream system rewrites files in place, you have two choices:
- Change the upstream process so it writes new file names. This is the better option because it keeps ingestion append-only.
- Set
cloudFiles.allowOverwritestotrue. Auto Loader then reprocesses a file when its modification time changes. The same file can produce rows more than once in the target, so you must deduplicate downstream.
Re-ingesting everything on purpose
To reload all history, for example after a bug in your transformation logic, the checkpoint must be new. Deleting or pointing to a new checkpoint makes the stream treat all files as unseen. Appending into the old target table then creates duplicates. A safe sequence is:
- Stop the stream.
- Write to a new target table, or truncate the existing one.
- Use a new
checkpointLocation. - Keep the same
schemaLocationif you want to keep the inferred schema and its evolution history. Use a new one if you want to infer it again. - Start the stream. It backfills everything.
Checkpoint state and long-lived streams
The checkpoint holds a record of every file the stream has seen. In a long-lived stream that list grows. Auto Loader stores it in a RocksDB state store inside the checkpoint, so lookups stay fast, but the state does not shrink unless you configure it to.
The option cloudFiles.maxFileAge tells Auto Loader how long to remember a file for deduplication. Once a file's record is older than that age, the stream may forget it. The Databricks documentation advises against using it as a routine setting. It is meant for streams that ingest very large numbers of files, in the order of millions per hour, and the value should be at least 90 days. If a forgotten file shows up again, the stream will ingest it again. Do not set this value unless you have measured a state-size problem.
Missed files in notification mode
The discovery modes were covered earlier. A backfill matters for them in one way. In file notification mode, discovery depends on events from cloud storage. Events can be delayed or, rarely, lost. A file whose event never arrives is never ingested, because the stream does not list the directory.
The option cloudFiles.backfillInterval closes this gap. It makes Auto Loader run a periodic asynchronous listing of the path and pick up files that notifications missed. Databricks recommends it for workloads that need completeness guarantees.
df = (spark.readStream
.format("cloudFiles")
.option("cloudFiles.format", "csv")
.option("header", "true")
.option("cloudFiles.schemaLocation", "/Volumes/main/raw/_schemas/events")
.option("cloudFiles.useNotifications", "true")
.option("cloudFiles.backfillInterval", "1 day")
.load("s3://my-bucket/landing/events"))A value of one day or one week is typical. A listing is expensive on a large path, so choose the longest interval that your completeness requirement allows. Directory listing mode does not need this option, because every trigger already lists the path.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| History was not loaded, only new files appear | `includeExistingFiles` set to `false`, or files fall outside `modifiedAfter` | Use a new checkpoint with the right setting |
| Changing `includeExistingFiles` has no effect | The checkpoint already has state | Create a new checkpoint |
| Duplicate rows after a restart | Checkpoint deleted or replaced while the target kept its data | Reset checkpoint and target together |
| Overwritten file not reflected in the table | `allowOverwrites` is `false` | Write new file names, or enable the option and deduplicate |
| First run takes hours before any batch appears | Initial listing of a very large path | Narrow the path, or accept the one-time cost |
| Some files never arrive in notification mode | Lost or delayed storage events | Set `cloudFiles.backfillInterval` |
Exercise
A stream was created with the default settings and has been running for a week. You now want to skip the history that it already ingested and re-create the table from only files modified after 1 December 2026. A colleague suggests setting cloudFiles.includeExistingFiles to false and restarting with the same checkpoint. Will that work, and what would you do instead?
Show answer
It will not work. includeExistingFiles is read only when the checkpoint is empty, and the existing checkpoint already records the files the stream has seen. Instead, stop the stream, create a new target table (or truncate the old one), and start the stream with a new checkpoint location and modifiedAfter set to "2026-12-01 00:00:00.000000 UTC+0". The new checkpoint makes the stream start fresh, and the filter limits the backfill to the files you want.
Exercise
A stream in file notification mode has run for months. An audit shows that a handful of files in the landing folder never reached the bronze table. Directory listing mode on the same data would not have this problem. What option addresses it, and what is its cost?
Show answer
Set cloudFiles.backfillInterval, for example to "1 day". Auto Loader then lists the directory asynchronously at that interval and ingests files whose storage events were lost or delayed. The cost is the listing itself, which can be slow and billable on a path with many files, so pick the longest interval that still meets your completeness requirement.