cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
cancel
Showing results for 
Search instead for 
Did you mean: 

How to quickly check if Delta Table is Empty

Retko
Contributor

Hi,

I need some quick way to return True if Delta Table is Empty.

Tried this, but is is quite slow when checking more tables.

spark.read.table("table_name").count()

spark.read.table("table_name").rdd.isEmpty()

len(spark.read.table("table_name").head(1)) == 0

Thank you.

1 ACCEPTED SOLUTION

Accepted Solutions

Anonymous
Not applicable

@Retko Okter​ :

Use the isEmpty method of the DataFrame API in Spark to check if a Delta table is empty

from delta.tables import DeltaTable
 
def is_delta_table_empty(table_name):
  delta_table = DeltaTable.forPath(spark, table_name)
  return delta_table.toDF().isEmpty()
 
# Usage example
if is_delta_table_empty("table_name"):
  print("Table is empty")
else:
  print("Table is not empty")

View solution in original post

2 REPLIES 2

Anonymous
Not applicable

@Retko Okter​ :

Use the isEmpty method of the DataFrame API in Spark to check if a Delta table is empty

from delta.tables import DeltaTable
 
def is_delta_table_empty(table_name):
  delta_table = DeltaTable.forPath(spark, table_name)
  return delta_table.toDF().isEmpty()
 
# Usage example
if is_delta_table_empty("table_name"):
  print("Table is empty")
else:
  print("Table is not empty")

Vartika
Moderator
Moderator

Hi @Retko Okter​ 

Just wanted to check in if you were able to resolve your issue. If yes, would you be happy to mark an answer as best? If not, please tell us so we can help you.

Thanks!

Welcome to Databricks Community: Lets learn, network and celebrate together

Join our fast-growing data practitioner and expert community of 80K+ members, ready to discover, help and collaborate together while making meaningful connections. 

Click here to register and join today! 

Engage in exciting technical discussions, join a group with your peers and meet our Featured Members.