{"record":{"id":"2908358a70a2bca4","repo":"apache/iceberg","slug":"location-already-exists-s-290835","errorCode":null,"errorMessage":"Location already exists: %s","messagePattern":"Location already exists: (.+?)","errorType":"exception","errorClass":"AlreadyExistsException","httpStatus":409,"severity":"error","filePath":"gcp/src/main/java/org/apache/iceberg/gcp/gcs/GCSOutputFile.java","lineNumber":66,"sourceCode":"      AutoCloseable gcsFileSystem,\n      BlobId blobId,\n      GCPProperties gcpProperties,\n      MetricsContext metrics) {\n    super(storage, gcsFileSystem, blobId, gcpProperties, metrics);\n  }\n\n  /**\n   * Create an output stream for the specified location if the target object does not exist in GCS\n   * at the time of invocation.\n   *\n   * @return output stream\n   */\n  @Override\n  public PositionOutputStream create() {\n    if (!exists()) {\n      return createOrOverwrite();\n    } else {\n      throw new AlreadyExistsException(\"Location already exists: %s\", uri());\n    }\n  }\n\n  @Override\n  public PositionOutputStream createOrOverwrite() {\n    try {\n      return new GCSOutputStream(storage(), blobId(), gcpProperties(), metrics());\n    } catch (IOException e) {\n      throw new UncheckedIOException(\"Failed to create output stream for location: \" + uri(), e);\n    }\n  }\n\n  @Override\n  public InputFile toInputFile() {\n    return new GCSInputFile(storage(), gcsFileSystem(), blobId(), null, gcpProperties(), metrics());\n  }\n}\n","sourceCodeStart":48,"sourceCodeEnd":84,"githubUrl":"https://github.com/apache/iceberg/blob/86d9c8fc543e7c56c9f624eb725f76c9baff9570/gcp/src/main/java/org/apache/iceberg/gcp/gcs/GCSOutputFile.java#L48-L84","documentation":"GCSOutputFile.create() throws AlreadyExistsException when the target GCS blob already exists. The library treats create() as strictly create-new: it checks blob existence first and refuses to overwrite, forcing callers to choose between create() and createOrOverwrite() explicitly. This prevents silent data loss from accidental overwrites.","triggerScenarios":"Calling GCSOutputFile.create() when a blob already exists at the file's URI; e.g. writing an output file to a path that a previous job already wrote.","commonSituations":"Re-running a batch/Spark job that writes to the same output path; concurrent writers racing to the same location; misconfigured output path pointing at an existing file.","solutions":["Delete the existing blob or use a unique/new output location before calling create()","Call createOrOverwrite() instead of create() if overwriting is intended","Enable unique/metadata-location output naming (e.g. include attempt ID or UUID in the path)","Handle AlreadyExistsException and pick an alternative location"],"exampleFix":"// before\nOutputFile out = table.io().newOutputFile(location);\nPositionOutputStream os = out.create();\n// after\nPositionOutputStream os = out.createOrOverwrite(); // or ensure location is fresh/delete blob first","handlingStrategy":"try-catch","validationCode":"if (((GCSOutputFile) out).exists()) { /* delete or pick new location */ }","typeGuard":null,"tryCatchPattern":"try { os = out.create(); } catch (AlreadyExistsException e) { os = out.createOrOverwrite(); /* or relocate */ }","preventionTips":["Write to unique per-attempt paths (UUID/attempt-id)","Delete stale output before rerunning jobs","Prefer createOrOverwrite() when overwrite semantics are intended"],"tags":["gcs","io","file-already-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"}