I wanted to change a column name of a Databricks Delta table.

So I did the following:

// Read old table data
val old_data_DF = spark.read.format("delta")

// Created a new DF with a renamed column
val new_data_DF = old_data_DF
      .withColumnRenamed("column_a", "metric1")

// Dropped and recereated the Delta files location
dbutils.fs.rm("dbfs:/mnt/main/sales", true)

// Trying to write the new DF to the location

Here I'm getting an Error at the last step when writing to Delta:

java.io.FileNotFoundException: dbfs:/mnt/main/sales/sale_date_partition=2019-04-29/part-00000-769.c000.snappy.parquet
A file referenced in the transaction log cannot be found. This occurs when data has been manually deleted from the file system rather than using the table `DELETE` statement

Obviously the data was deleted and most likely I've missed something in the above logic. Now the only place that contains the data is the new_data_DF. Writing to a location like dbfs:/mnt/main/sales_tmp also fails

What should I do to write data from new_data_DF to a Delta location?

1 Answers

Michael Armbrust On

In general, it is a good idea to avoid using rm on Delta tables. Delta's transaction log can prevent eventual consistency issues in most cases, however, when you delete and recreate a table in a very short time, different versions of the transaction log can flicker in and out of existence.

Instead, I'd recommend using the transactional primitives provided by Delta. For example, to overwrite the data in a table you can:


If you have a table that has already been corrupted, you can fix it using FSCK.