POST_START
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Removing All Rows While Preserving a Table with TRUNCATE</title>
</head>
<body>
<h1>Removing All Rows While Preserving a Table with TRUNCATE</h1>
<p>I recently had a need to clear out all the data from a table while keeping the table structure intact. The table in question was <code>training.sales.customer_master</code>, and I needed to ensure that the table itself wasn’t deleted or altered in any way. The goal was to remove all rows efficiently and verify that the table was indeed empty after the operation.</p>
<h2>Understanding the Need</h2>
<p>I started by recalling the different ways to remove data from a table in Databricks. While <code>DELETE</code> is a common method, it can be slow on large tables due to the logging and transaction overhead. I also remembered that <code>TRUNCATE</code> is a more efficient operation for removing all rows, as it resets the table's high water mark and doesn’t generate as much log data.</p>
<p>However, I also needed to be cautious. <code>TRUNCATE</code> is a DDL (Data Definition Language) operation, which means it immediately drops and re-creates the table internally, which can have implications on dependent objects like views or foreign key constraints. I wanted to make sure that the table was still present and accessible after the operation.</p>
<h2>Planning the Steps</h2>
<p>I decided to follow a two-step approach: first, I would run the <code>TRUNCATE TABLE</code> command, and then I would verify the result by checking the number of rows in the table. This would allow me to confirm that all data was removed and that the table structure was preserved.</p>
<p>I also made sure to work within the <code>training.sales</code> schema, as that's where the table <code>customer_master</code> resides. I wanted to avoid any accidental modifications to other tables or schemas.</p>
<h2>Executing the Truncate</h2>
<p>I opened the Databricks notebook and navigated to the appropriate cell. I then executed the following command:</p>
<pre><code>TRUNCATE TABLE training.sales.customer_master;</code></pre>
<p>I noticed that the operation completed almost instantly, which was a clear indication that it was more efficient than using <code>DELETE</code>. I also observed that the table was still present in the metadata, which confirmed that the table structure wasn’t altered.</p>
<p>One thing I learned was that <code>TRUNCATE</code> doesn't generate a transaction log entry for each row, which makes it faster and less resource-intensive. However, it does require that the table has no foreign key constraints that are set to <code>ON DELETE CASCADE</code>, or that any such constraints are properly managed.</p>
<h2>Verifying the Result</h2>
<p>Next, I wanted to confirm that all rows had been removed. I ran the following query:</p>
<pre><code>SELECT COUNT(*) FROM training.sales.customer_master;</code></pre>
<p>The result was <code>0</code>, which meant that the table was now empty. This step was crucial to ensure that the operation had been successful and that there were no lingering rows that could cause issues in downstream processes.</p>
<p>I also checked the table metadata using the Databricks UI to confirm that the table structure remained unchanged. This gave me confidence that the <code>TRUNCATE</code> operation had preserved the table's schema and only removed the data.</p>
<h2>Conclusion</h2>
<p>By using <code>TRUNCATE TABLE</code>, I was able to efficiently remove all rows from the <code>training.sales.customer_master</code> table without altering its structure. This operation proved to be both fast and reliable for my use case. I now understand when and how to use <code>TRUNCATE</code> effectively, and I've added it to my toolkit for data management tasks in Databricks.</p>
<p>For future reference, I'll always verify the results of such operations using a <code>SELECT COUNT(*)</code> to ensure that the data has been removed as expected.</p>
</body>
</html>


Leave a Reply