Sub topic 9
Changing Rows with UPDATE and DELETE
This chapter assumes you can write basic SQL, know what a Delta table is, and have read the earlier chapters on the transaction log and on appending and overwriting data. You will change and remove rows in place, and you will see what Delta does internally when you do.
Why UPDATE and DELETE matter
Plain files on cloud storage cannot be edited in place. Delta Lake adds that ability. You use standard SQL, and each statement commits as one atomic table version. A reader sees the table either before the statement or after it, never halfway through.
Running UPDATE and DELETE
UPDATE main.sales.orders
SET status = 'shipped'
WHERE order_id = 1001;
DELETE FROM main.sales.orders
WHERE status = 'cancelled';UPDATE sets new values for the rows that match the WHERE clause. DELETE removes the matching rows. If you leave out WHERE, the statement affects every row in the table.
Both statements return a row count. In a notebook you can read it in the result grid (num_affected_rows). Check it. A count of 0 usually means your condition matched nothing.
What happens inside the table
Delta does not modify data files. Parquet files are immutable, so Delta rewrites them.
- Delta finds the data files that contain matching rows.
- It writes new files that hold the unchanged rows from those files plus the updated rows. Deleted rows are left out.
- It commits one entry to the transaction log. The entry marks the old files as removed and the new files as added.
The old files stay in storage until VACUUM removes them. That is why time travel works after a change.
Some tables use deletion vectors. In that case Delta may record which rows are deleted instead of rewriting the whole file right away. The result you see is the same, and the file rewrite happens later during maintenance.
Writing efficient conditions
Rewriting is done per file, so a selective condition matters.
- A condition on a partition column or a clustering column lets Delta skip files that cannot match. Fewer files are rewritten.
- A condition on a column with no useful layout may touch many files, even if few rows match.
- A single matching row still forces its whole file to be rewritten, unless deletion vectors are on.
Common errors
- Missing
WHERE. The statement changes the whole table. You can undo it withRESTORE, but check the condition before you run it. - Wrong
NULLlogic.WHERE status <> 'new'does not match rows wherestatusisNULL. Usestatus IS NULL OR status <> 'new'if you want those rows too. - Breaking a constraint. An
UPDATEthat sets aNOT NULLcolumn toNULL, or violates aCHECKconstraint, fails. The whole statement is rolled back and no rows change. - Expecting a smaller table right after a delete. Removed files remain in storage until
VACUUMruns, so storage use does not drop immediately. - Concurrent conflicts. If another job rewrites the same files while your statement runs, one of the writers can fail with a conflict error. Rerun the statement. A later chapter covers this.
Checking the result
After a change, look at the history to confirm what happened.
DESCRIBE HISTORY main.sales.orders;Each UPDATE or DELETE appears as a new version with its operation name and metrics such as the number of rows affected. The next chapters use this history for time travel and RESTORE.
Exercise
You run DELETE FROM main.sales.orders WHERE status = 'cancelled' and the command succeeds, but the storage size of the table does not shrink. Why, and what removes the old data?
Show answer
Delta does not delete the old data files. The commit only marks them as removed in the transaction log, and the files stay in storage so that time travel and concurrent readers keep working. VACUUM deletes files that are no longer referenced and are older than the retention period.
Exercise
A table has 1,000 rows, and 50 of them have status set to NULL. You run DELETE FROM t WHERE status <> 'new'. Are the 50 NULL rows deleted? How would you include them?
Show answer
No. The comparison NULL <> 'new' evaluates to unknown, so those rows do not match. Use WHERE status IS NULL OR status <> 'new' to include them.