cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

How to delete duplicate tables?

Mahesh777k
New Contributor

Hi Everyone,

Accidently imported duplicate tables, guide me how to delete them

using data bricks community edition

image 

2 REPLIES 2

Chaitanya_Raju
Honored Contributor

Hi @Mahesh Babu Uppalaโ€‹ ,

You can use the below command to delete the particular file

dbutils.fs.rm("path of the file")

If you want to delete the entire directory where it consists of sub-directories and files, you can use the below command to delete the files recursively

dbutils.fs.rm("path of the folder",True)

After executing the above commands you will be getting the output below, to confirm file/directory got deleted successfully.

"Boolean = true"

Happy Learning!!

Thanks for reading and like if this is useful and for improvements or feedback please comment.

UmaMahesh1
Honored Contributor III

Hi @Mahesh Babu Uppalaโ€‹ 

You can use the following method to delete only the duplicate tables

%scala
val tables = spark.sql("""SHOW TABLES""").createOrReplaceTempView("tables")
val temp_tables = spark.sql("""select tableName from tables where tableName like '%-1%' """)
temp_tables.collect().foreach(row => println("DROP TABLE IF EXISTS " + row.toString().replace("[", "").replace("]", "") + ";"))

You will get the sql command in a string...you can either copy this cell output to another cell directly and run it or you can automate the process by storing this output into a variable and then calling it through a loop...Better way would be to simply copy the output and execute it.

Hope this helps..Cheers...

Uma Mahesh D

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you wonโ€™t want to miss the chance to attend and share knowledge.

If there isnโ€™t a group near you, start one and help create a community that brings people together.

Request a New Group