{"record":{"id":"88a8659a29bbcafe","repo":"apache/iceberg","slug":"failed-to-delete-s","errorCode":null,"errorMessage":"Failed to delete: %s","messagePattern":"Failed to delete: (.+?)","errorType":"exception","errorClass":"RuntimeIOException","httpStatus":null,"severity":"error","filePath":"api/src/main/java/org/apache/iceberg/Files.java","lineNumber":77,"sourceCode":"        throw new AlreadyExistsException(\"File already exists: %s\", file);\n      }\n\n      if (!file.getParentFile().isDirectory() && !file.getParentFile().mkdirs()) {\n        throw new RuntimeIOException(\n            \"Failed to create the file's directory at %s.\", file.getParentFile().getAbsolutePath());\n      }\n\n      try {\n        return new PositionFileOutputStream(file, new RandomAccessFile(file, \"rw\"));\n      } catch (FileNotFoundException e) {\n        throw new NotFoundException(e, \"Failed to create file: %s\", file);\n      }\n    }\n\n    @Override\n    public PositionOutputStream createOrOverwrite() {\n      if (file.exists() && !file.delete()) {\n        throw new RuntimeIOException(\"Failed to delete: %s\", file);\n      }\n      return create();\n    }\n\n    @Override\n    public String location() {\n      return file.toString();\n    }\n\n    @Override\n    public InputFile toInputFile() {\n      return localInput(file);\n    }\n\n    @Override\n    public String toString() {\n      return location();\n    }","sourceCodeStart":59,"sourceCodeEnd":95,"githubUrl":"https://github.com/apache/iceberg/blob/86d9c8fc543e7c56c9f624eb725f76c9baff9570/api/src/main/java/org/apache/iceberg/Files.java#L59-L95","documentation":"createOrOverwrite() deletes an existing file before delegating to create(). If File.delete() fails when the file exists, it throws RuntimeIOException; on POSIX delete can fail with open handles or missing write permission on the parent directory.","triggerScenarios":"Calling createOrOverwrite() on a path whose existing file cannot be deleted — file is locked/open by another process/thread, parent directory lacks write permission, or filesystem denies unlink (e.g. read-only mount, stale NFS handle).","commonSituations":"Two concurrent jobs writing the same metadata/data path; Windows file locking by an indexer/AV or an unclosed stream in the same JVM; container with read-only layer; NFS semantics delays.","solutions":["Ensure no other process or stream holds the file open (close prior OutputStreams; serialize writers)","Check parent-directory write permission, required to unlink entries on POSIX filesystems","If overwrites are unsafe, switch to unique per-attempt file names instead of overwriting"],"exampleFix":"// before\nPositionOutputStream s = fileIO.newOutputFile(fixedPath).createOrOverwrite();\n// after\nFile f = new File(fixedPath);\nif (f.exists() && !f.delete()) {\n  throw new IllegalStateException(\"File locked or undeletable, pick a new path: \" + f);\n}\nPositionOutputStream s = fileIO.newOutputFile(fixedPath).createOrOverwrite();","handlingStrategy":"validation","validationCode":"File f = new File(URI.create(location).getPath());\nif (f.exists()) {\n  Preconditions.checkState(f.delete(), \"Undeletable file: %s\", f);\n}","typeGuard":null,"tryCatchPattern":"try {\n  stream = outputFile.createOrOverwrite();\n} catch (RuntimeIOException e) {\n  throw new IllegalStateException(\"Delete failed; file may be locked: \" + e.getMessage(), e);\n}","preventionTips":["Serialize writers to the same path; avoid concurrent overwrite","Close all output streams before attempting overwrite","Ensure parent directory grants write permission (needed to unlink)"],"tags":["java","fileio","io","permissions"],"backgroundTag":"file-write-failed","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"}