The maintenance notebook is three lines, it ran fine for a month, and now it fails most nights with a concurrent modification error. Nothing changed in the notebook. What changed is that the streaming job feeding the same table got busier.

-- nightly maintenance notebook (Spark SQL), 02:00 OPTIMIZE silver_events; VACUUM silver_events; DESCRIBE HISTORY silver_events LIMIT 5; 

Why it only breaks sometimes

If your background is SQL Server, this is the assumption that bites. SQL Agent jobs serialise against a table using locks: the second writer waits. Delta does not. Delta uses optimistic concurrency — both writers proceed, and the one that commits second checks whether the files it read are still current. If they are not, it fails rather than corrupting the table.

So a nightly OPTIMIZE rewriting files while a streaming MERGE is writing into the same files is not a queue. It is a race, and the loser raises a concurrent modification error. It passes on quiet nights and fails on busy ones, which is why it looks intermittent and gets re-run rather than fixed.

The fix is a predicate, not a schedule

The instinct is to stop the stream during maintenance. Usually you cannot — the whole point of the stream is that it does not stop.

You do not need to. OPTIMIZE takes a partition predicate, and the stream only writes to recent partitions:

OPTIMIZE silver_events WHERE event_date < current_date(); 

Compaction now touches only the partitions the streaming MERGE has finished with. The two jobs never rewrite the same files, so there is nothing to collide over. Today's partition stays fragmented until tomorrow, which is the correct trade: the small files that matter for query performance are the historical ones users actually scan.

This only works if the predicate is on the partition column. A filter on a non-partition column does not restrict which files OPTIMIZE rewrites, and you are back in the race.

Two adjacent mistakes

VACUUM is not compaction. It deletes files no Delta version references any more. If all 40,210 of your small files belong to the current version, VACUUM removes nothing and the table keeps slowing down. It is a storage-cost tool, not a performance tool — useful, but not for this.

Partitioning is not Z-ordering. When a query filters on a high-cardinality column, the temptation is to partition by it:

SELECT event_id, event_type, payload FROM events WHERE event_date BETWEEN '2026-10-01' AND '2026-10-31' AND customer_id = 'C-104233'; 

Partitioning on customer_id with eight million distinct values creates millions of tiny folders inside each date — the exact layout you were trying to escape. ZORDER BY (customer_id) instead co-locates each customer's rows into a few files within every event_date partition, so file statistics let the filter skip most files.

V-Order is a third, separate thing: it improves encoding for readers, but without Z-order the rows for one customer stay spread across every file in the partition, and few files can be skipped.

What a working maintenance job looks like

OPTIMIZE silver_events WHERE event_date < current_date() ZORDER BY (customer_id); VACUUM silver_events RETAIN 168 HOURS; 

Scoped so it cannot race the stream, Z-ordered on the column people actually filter by, and vacuuming on a retention window rather than hoping it compacts something.

The general rule, once you stop thinking in locks: on Delta, two writers are safe when they touch disjoint sets of files, and only then. Every concurrency error on a Delta table is really a question about which files each job rewrites.

Documented in Table compaction and Delta Lake table optimization and V-Order.

I write DP-700 practice scenarios for a living. This exact case is one of ten free diagnostic questions, no sign-up: https://gyanagent.in/exams/dp-700