Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Truncate delta table in Databricks using python

Delta table delete operation is given here for Python and SQL, and truncate using SQL is given here. But I cannot find the documentation for Python truncate table.

How to do it for delta table in Databricks?

like image 545
Blue Clouds Avatar asked Sep 04 '26 20:09

Blue Clouds


1 Answers

Not everything is exposed as a function for Python or Java/Scala. Some operations are SQL-only, like OPTIMIZE for example. If you want to truncate table, you have two choices:

  1. Use
spark.sql("TRUNCATE TABLE <name>")

or

spark.sql("TRUNCATE TABLE delta.`<path>`")
  1. Emulate truncate with read + write empty dataframe in overwrite mode:
df = spark.read.format("delta").load("<path>")
df.limit(0).write.mode("overwrite").format("delta").save("<path>")
like image 63
Alex Ott Avatar answered Sep 06 '26 09:09

Alex Ott



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!