{"record":{"id":"ed29e94b4f839c59","repo":"apache/flink","slug":"file-already-exists","errorCode":null,"errorMessage":"File already exists: {}","messagePattern":"File already exists: (.+?)","errorType":"exception","errorClass":"FileAlreadyExistsException","httpStatus":null,"severity":"error","filePath":"flink-core/src/main/java/org/apache/flink/core/fs/local/LocalFileSystem.java","lineNumber":256,"sourceCode":"            // Important: The 'exists()' check above must come before the 'isDirectory()' check to\n            //            be safe when multiple parallel instances try to create the directory\n\n            // exists and is not a directory -> is a regular file\n            throw new FileAlreadyExistsException(file.getAbsolutePath());\n        } else {\n            File parent = file.getParentFile();\n            return (parent == null || mkdirsInternal(parent))\n                    && (file.mkdir() || file.isDirectory());\n        }\n    }\n\n    @Override\n    public FSDataOutputStream create(final Path filePath, final WriteMode overwrite)\n            throws IOException {\n        checkNotNull(filePath, \"filePath\");\n\n        if (exists(filePath) && overwrite == WriteMode.NO_OVERWRITE) {\n            throw new FileAlreadyExistsException(\"File already exists: \" + filePath);\n        }\n\n        final Path parent = filePath.getParent();\n        if (parent != null && !mkdirs(parent)) {\n            throw new IOException(\"Mkdirs failed to create \" + parent);\n        }\n\n        final File file = pathToFile(filePath);\n        return new LocalDataOutputStream(file);\n    }\n\n    @Override\n    public boolean rename(final Path src, final Path dst) throws IOException {\n        final File srcFile = pathToFile(src);\n        final File dstFile = pathToFile(dst);\n\n        final File dstParent = dstFile.getParentFile();\n","sourceCodeStart":238,"sourceCodeEnd":274,"githubUrl":"https://github.com/apache/flink/blob/2f3c205e9266cb30240eb7f4fdab15cad629a70f/flink-core/src/main/java/org/apache/flink/core/fs/local/LocalFileSystem.java#L238-L274","documentation":"Thrown as FileAlreadyExistsException by LocalFileSystem.create() when the target filePath already exists AND WriteMode.NO_OVERWRITE is set. It guards against silently clobbering existing data on create.","triggerScenarios":"Calling create(path, WriteMode.NO_OVERWRITE) on a path where exists(path) is true.","commonSituations":"Output file/dir from a previous run still present; re-running a job without clearing output; sink configured for no-overwrite but target not cleaned.","solutions":["Delete the existing target before creating: `fs.delete(path, true)` then create.","Use WriteMode.OVERWRITE if clobbering is acceptable: `fs.create(path, WriteMode.OVERWRITE)`.","Use a unique output path per run (timestamp/uuid suffix).","Add a startup cleanup step in the job."],"exampleFix":"// before\nfs.create(path, WriteMode.NO_OVERWRITE);\n\n// after\nif (fs.exists(path)) fs.delete(path, true);\nfs.create(path, WriteMode.NO_OVERWRITE);","handlingStrategy":"validation","validationCode":"FSDataOutputStream safeCreate(FileSystem fs, Path p, WriteMode mode) throws IOException {\n    if (mode == WriteMode.NO_OVERWRITE && fs.exists(p))\n        throw new FileAlreadyExistsException(\"will not overwrite: \" + p);\n    return fs.create(p, mode);\n}","typeGuard":null,"tryCatchPattern":"try {\n    fs.create(p, WriteMode.NO_OVERWRITE);\n} catch (FileAlreadyExistsException e) {\n    fs.delete(p, true);\n    fs.create(p, WriteMode.NO_OVERWRITE);\n}","preventionTips":["Clean output targets before job runs or use unique paths per run.","Choose WriteMode.OVERWRITE only when clobbering is safe.","Make output path uniqueness part of job config."],"tags":["filesystem","local-fs","create","overwrite","configuration"],"backgroundTag":null,"analyzedSha":"2f3c205e9266cb30240eb7f4fdab15cad629a70f","analyzedAt":"2026-08-14T08:48:24.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}