Forum Discussion

Soobramoney's avatar
Soobramoney
Icon for Advocate I rankAdvocate I
1 year ago
Solved

Lakehouse backup table

Hi,   I have a data pipeline that loads data into a staging table with the destination setting set to "Overwrite." This seems to create a _backup_XXXXXXXXXX table each time I load data. Prior to us...
  • Soobramoney's avatar
    1 year ago

    It seems you cannot prevent the backup tables when you are using the Overwrite setting. I had to create a clean job that runs once a day.

     

    import logging
    logging.basicConfig(level=logging.INFO)
    logger = logging.getLogger("BackupTableCleanup")
    
    backup_tables = spark.sql("SHOW TABLES")
    
    
    drop_backup_tables = backup_tables.filter(
    (backup_tables["tableName"].like('%_backup_%')) &
    )
    
    
    drop_statements = drop_backup_tables.rdd.map(lambda row: f"DROP TABLE {row['tableName']};").collect()
    
    for statement in drop_statements:
    try:
    logger.info(f"Executing: {statement}")
    print(f"Executing: {statement}")
    spark.sql(statement)
    
    logger.info(f"Successfully executed: {statement}")
    except Exception as e:
    logger.error(f"Failed to execute: {statement}. Error: {e}")