简体   繁体   中英

Truncate delta table in Databricks using python

Delta table delete operation is given here for Python and SQL. Truncate using SQL is given here . But cannot find a documentation for Python truncate table. How to do it for delta table in Databricks?

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>")
query = "TRUNCATE TABLE <name>"

sqlContext.sql(query)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM