{"record":{"id":"7bc56b4a9b12eef6","repo":"apache/iceberg","slug":"cannot-rename-s-as-s-for-backup-the-backup-tabl-7bc56b","errorCode":null,"errorMessage":"Cannot rename %s as %s for backup. The backup table already exists.","messagePattern":"Cannot rename (.+?) as (.+?) for backup\\. The backup table already exists\\.","errorType":"exception","errorClass":"org.apache.iceberg.exceptions.AlreadyExistsException","httpStatus":null,"severity":"error","filePath":"spark/v4.2/spark/src/main/java/org/apache/iceberg/spark/actions/MigrateTableSparkAction.java","lineNumber":242,"sourceCode":"    Preconditions.checkArgument(\n        catalog instanceof SparkSessionCatalog,\n        \"Cannot migrate a table from a non-Iceberg Spark Session Catalog. Found %s of class %s as the source catalog.\",\n        catalog.name(),\n        catalog.getClass().getName());\n\n    return (TableCatalog) catalog;\n  }\n\n  private void renameAndBackupSourceTable() {\n    try {\n      LOG.info(\"Renaming {} as {} for backup\", sourceTableIdent(), backupIdent);\n      destCatalog().renameTable(sourceTableIdent(), backupIdent);\n\n    } catch (org.apache.spark.sql.catalyst.analysis.NoSuchTableException e) {\n      throw new NoSuchTableException(\"Cannot find source table %s\", sourceTableIdent());\n\n    } catch (org.apache.spark.sql.catalyst.analysis.TableAlreadyExistsException e) {\n      throw new AlreadyExistsException(\n          \"Cannot rename %s as %s for backup. The backup table already exists.\",\n          sourceTableIdent(), backupIdent);\n    }\n  }\n\n  private void restoreSourceTable() {\n    try {\n      LOG.info(\"Restoring {} from {}\", sourceTableIdent(), backupIdent);\n      destCatalog().renameTable(backupIdent, sourceTableIdent());\n\n    } catch (org.apache.spark.sql.catalyst.analysis.NoSuchTableException e) {\n      LOG.error(\n          \"Cannot restore the original table, the backup table {} cannot be found\", backupIdent, e);\n\n    } catch (org.apache.spark.sql.catalyst.analysis.TableAlreadyExistsException e) {\n      LOG.error(\n          \"Cannot restore the original table, a table with the original name exists. \"\n              + \"Use the backup table {} to restore the original table manually.\",","sourceCodeStart":224,"sourceCodeEnd":260,"githubUrl":"https://github.com/apache/iceberg/blob/86d9c8fc543e7c56c9f624eb725f76c9baff9570/spark/v4.2/spark/src/main/java/org/apache/iceberg/spark/actions/MigrateTableSparkAction.java#L224-L260","documentation":"During MIGRATE, the source table is renamed to a backup name before the new Iceberg table is created. If a table already exists under the backup identifier, destCatalog().renameTable throws TableAlreadyExistsException, converted into this AlreadyExistsException. The migration aborts rather than overwriting the backup.","triggerScenarios":"Running MIGRATE when a table with the auto-generated (or previously used) backup identifier already exists in the destination catalog, e.g. from an earlier failed/aborted migrate run.","commonSituations":"Re-running MIGRATE after a previous failure left the backup table in place; another table happens to share the backup name pattern.","solutions":["Drop or rename the leftover backup table, restore it if needed, then re-run MIGRATE","If the backup is the migrated result of a previous run, restore the source from the backup instead of migrating again","Pick a source identifier that yields a unique backup name"],"exampleFix":"// before\nCALL iceberg.system.migrate('db.src_tbl') -- backup db.SRC_TBL_BACKUP__... exists\n// after\nDROP TABLE db.src_tbl_backup; -- after verifying it is disposable\nCALL iceberg.system.migrate('db.src_tbl');","handlingStrategy":"validation","validationCode":"// Check for leftover backup tables before re-running MIGRATE\nspark.sql(\"SHOW TABLES IN db LIKE '*BACKUP*'\");","typeGuard":null,"tryCatchPattern":"try { migrateAction.execute(); } catch (AlreadyExistsException e) { /* inspect and remove/restore the existing backup table, then retry */ }","preventionTips":["After failed MIGRATE runs, check for and resolve leftover backup tables before retrying","Restore the source from a backup only after verifying its contents","Avoid running MIGRATE twice concurrently on the same source"],"tags":["spark","table-migration","rename","backup-exists"],"backgroundTag":"file-already-exists","analyzedSha":"86d9c8fc543e7c56c9f624eb725f76c9baff9570","analyzedAt":"2026-09-12T00:46:39.097Z","contentChangedAt":"2026-09-12T00:46:39.097Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}