{"record":{"id":"22738f25f8a1a559","repo":"apache/iceberg","slug":"cannot-create-table-s-as-it-already-exists-22738f","errorCode":null,"errorMessage":"Cannot create table %s as it already exists","messagePattern":"Cannot create table (.+?) as it already exists","errorType":"exception","errorClass":"AlreadyExistsException","httpStatus":null,"severity":"error","filePath":"spark/v4.1/spark/src/main/java/org/apache/iceberg/spark/actions/BaseTableCreationSparkAction.java","lineNumber":167,"sourceCode":"        SparkSessionCatalog.class.getName(),\n        SparkCatalog.class.getName());\n\n    return (StagingTableCatalog) catalog;\n  }\n\n  protected StagedSparkTable stageDestTable() {\n    try {\n      Map<String, String> props = destTableProps();\n      return (StagedSparkTable)\n          destCatalog()\n              .stageCreate(\n                  destTableIdent(),\n                  Spark3Util.tableInfo(sourceTable.columns(), sourceTable.partitioning(), props));\n    } catch (org.apache.spark.sql.catalyst.analysis.NoSuchNamespaceException e) {\n      throw new NoSuchNamespaceException(\n          \"Cannot create table %s as the namespace does not exist\", destTableIdent());\n    } catch (org.apache.spark.sql.catalyst.analysis.TableAlreadyExistsException e) {\n      throw new AlreadyExistsException(\n          \"Cannot create table %s as it already exists\", destTableIdent());\n    }\n  }\n\n  protected void ensureNameMappingPresent(Table table) {\n    if (!table.properties().containsKey(TableProperties.DEFAULT_NAME_MAPPING)) {\n      NameMapping nameMapping = MappingUtil.create(table.schema());\n      String nameMappingJson = NameMappingParser.toJson(nameMapping);\n      table.updateProperties().set(TableProperties.DEFAULT_NAME_MAPPING, nameMappingJson).commit();\n    }\n  }\n\n  protected String getMetadataLocation(Table table) {\n    String defaultValue =\n        LocationUtil.stripTrailingSlash(table.location()) + \"/\" + ICEBERG_METADATA_FOLDER;\n    return LocationUtil.stripTrailingSlash(\n        table.properties().getOrDefault(TableProperties.WRITE_METADATA_LOCATION, defaultValue));\n  }","sourceCodeStart":149,"sourceCodeEnd":185,"githubUrl":"https://github.com/apache/iceberg/blob/86d9c8fc543e7c56c9f624eb725f76c9baff9570/spark/v4.1/spark/src/main/java/org/apache/iceberg/spark/actions/BaseTableCreationSparkAction.java#L149-L185","documentation":"This error is thrown when a Spark table-creation action (e.g. snapshot/migrate) attempts to create the destination Iceberg table but the catalog reports the table already exists. The Spark catalyst TableAlreadyExistsException is translated into Iceberg's AlreadyExistsException with the destination identifier in the message.","triggerScenarios":"Running a snapshot/migrate Spark action via stageDestTable when destTableIdent() already exists in destCatalog; e.g. re-running a partially completed migration where the destination table was created in a prior run.","commonSituations":"Re-running an interrupted MigrateTable/SnapshotTable action without dropping the partially created destination table; naming collision with an existing table in the target catalog/namespace.","solutions":["Drop the existing destination table or choose a new destTableIdent for the action","Check whether a previous migration run partially completed and finish or clean it up first","Verify you are targeting the intended catalog and namespace to avoid a name collision"],"exampleFix":"// before: re-running same command\ndropTable(\"db.my_table\"); actions.migrateTable(\"db.my_table\");\n// after: use a fresh destination or drop first\nspark.sql(\"DROP TABLE IF EXISTS catalog.db.my_table\"); actions.migrateTable(\"db.my_table\");","handlingStrategy":"try-catch","validationCode":"boolean exists = catalog.tableExists(destIdent);\nif (exists) { throw new IllegalStateException(\"Destination already exists: \" + destIdent); }","typeGuard":null,"tryCatchPattern":"try { action.execute(); } catch (AlreadyExistsException e) { /* drop or rename destTableIdent, then retry */ }","preventionTips":["Check catalog.tableExists(destIdent) before running snapshot/migrate actions","Use unique destination identifiers per run or clean up after failed runs","Inspect prior failed migrations for leftover destination tables"],"tags":["spark","iceberg","table-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-14T16:17:12.679Z"}