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 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
Databricks Employee
Databricks Employee

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!

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