<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Recommended approach for handling deletes in a Delta table in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/recommended-approach-for-handling-deletes-in-a-delta-table/m-p/128539#M48281</link>
    <description>&lt;P&gt;What is the recommended approach for handling deletes in a Delta table?&lt;BR /&gt;I have a table in MySQL (no soft delete flag) that I read and write into Azure as a Delta table. My current flow is:&lt;BR /&gt;- If an ID exists in both MySQL and the Delta table → update the record in Delta.&lt;BR /&gt;- If an ID exists in MySQL but not in Delta → insert it into Delta.&lt;BR /&gt;The challenge is: how do I handle deletes? Specifically, if an ID exists in the Delta table but does not exist in the latest MySQL extract, I want to remove it from Delta. What’s the best way to implement this logic?&lt;/P&gt;</description>
    <pubDate>Fri, 15 Aug 2025 14:25:11 GMT</pubDate>
    <dc:creator>WiliamRosa</dc:creator>
    <dc:date>2025-08-15T14:25:11Z</dc:date>
    <item>
      <title>Recommended approach for handling deletes in a Delta table</title>
      <link>https://community.databricks.com/t5/data-engineering/recommended-approach-for-handling-deletes-in-a-delta-table/m-p/128539#M48281</link>
      <description>&lt;P&gt;What is the recommended approach for handling deletes in a Delta table?&lt;BR /&gt;I have a table in MySQL (no soft delete flag) that I read and write into Azure as a Delta table. My current flow is:&lt;BR /&gt;- If an ID exists in both MySQL and the Delta table → update the record in Delta.&lt;BR /&gt;- If an ID exists in MySQL but not in Delta → insert it into Delta.&lt;BR /&gt;The challenge is: how do I handle deletes? Specifically, if an ID exists in the Delta table but does not exist in the latest MySQL extract, I want to remove it from Delta. What’s the best way to implement this logic?&lt;/P&gt;</description>
      <pubDate>Fri, 15 Aug 2025 14:25:11 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/recommended-approach-for-handling-deletes-in-a-delta-table/m-p/128539#M48281</guid>
      <dc:creator>WiliamRosa</dc:creator>
      <dc:date>2025-08-15T14:25:11Z</dc:date>
    </item>
    <item>
      <title>Re: Recommended approach for handling deletes in a Delta table</title>
      <link>https://community.databricks.com/t5/data-engineering/recommended-approach-for-handling-deletes-in-a-delta-table/m-p/128546#M48285</link>
      <description>&lt;P&gt;The recommended way of handling CDC in Databricks is by using the merge command.&lt;/P&gt;&lt;P&gt;&lt;A href="https://docs.databricks.com/aws/en/sql/language-manual/delta-merge-into" target="_blank" rel="noopener"&gt;https://docs.databricks.com/aws/en/sql/language-manual/delta-merge-into&lt;/A&gt;&lt;/P&gt;&lt;P&gt;If you using SQL.&lt;/P&gt;&lt;P&gt;-- Delete all target rows that have a match in the source table.&lt;BR /&gt;&amp;gt; MERGE INTO target USING source&lt;BR /&gt;ON target.key = source.key&lt;BR /&gt;WHEN MATCHED THEN DELETE&lt;/P&gt;&lt;P&gt;-- Conditionally update target rows that have a match in the source table using the source value.&lt;BR /&gt;&amp;gt; MERGE INTO target USING source&lt;BR /&gt;ON target.key = source.key&lt;BR /&gt;WHEN MATCHED AND target.updated_at &amp;lt; source.updated_at THEN UPDATE SET *&lt;/P&gt;&lt;P&gt;-- Multiple MATCHED clauses conditionally deleting matched target rows and updating two columns for all other matched rows.&lt;BR /&gt;&amp;gt; MERGE INTO target USING source&lt;BR /&gt;ON target.key = source.key&lt;BR /&gt;WHEN MATCHED AND target.marked_for_deletion THEN DELETE&lt;BR /&gt;WHEN MATCHED THEN UPDATE SET target.updated_at = source.updated_at, target.value = DEFAULT&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;-- if you are using python,&lt;/P&gt;&lt;P&gt;&lt;SPAN class=""&gt;from&lt;/SPAN&gt; &lt;SPAN class=""&gt;delta&lt;/SPAN&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;SPAN class=""&gt;tables&lt;/SPAN&gt; &lt;SPAN class=""&gt;import&lt;/SPAN&gt;&lt;SPAN&gt; *&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN class=""&gt;# Assuming 'targetTable' is a DeltaTable object and 'sourceDF' is a DataFrame&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN class=""&gt;# representing the data to be merged.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN class=""&gt;deltaTable&lt;/SPAN&gt;&lt;SPAN&gt; = DeltaTable.forPath(spark, &lt;/SPAN&gt;&lt;SPAN class=""&gt;"&lt;/SPAN&gt;&lt;SPAN class=""&gt;/path/to/your/delta/table&lt;/SPAN&gt;&lt;SPAN class=""&gt;"&lt;/SPAN&gt;&lt;SPAN&gt;) &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;deltaTable.merge(&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;sourceDF,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN class=""&gt;"&lt;/SPAN&gt;&lt;SPAN class=""&gt;target.key_column = source.key_column&lt;/SPAN&gt;&lt;SPAN class=""&gt;"&lt;/SPAN&gt;&amp;nbsp;&lt;BR /&gt;&lt;SPAN&gt;) &lt;/SPAN&gt;&lt;SPAN&gt;.whenMatchedUpdateAll() &lt;/SPAN&gt;&lt;SPAN&gt;.whenNotMatchedInsertAll().whenNotMatchedBySourceDelete()&lt;/SPAN&gt;&lt;SPAN&gt;.execute()&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 15 Aug 2025 15:16:21 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/recommended-approach-for-handling-deletes-in-a-delta-table/m-p/128546#M48285</guid>
      <dc:creator>nayan_wylde</dc:creator>
      <dc:date>2025-08-15T15:16:21Z</dc:date>
    </item>
  </channel>
</rss>

