apache/flink · error · FileAlreadyExistsException
File already exists: {}
Error message
File already exists: {} What it means
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.
Source
Thrown at flink-core/src/main/java/org/apache/flink/core/fs/local/LocalFileSystem.java:256
// Important: The 'exists()' check above must come before the 'isDirectory()' check to
// be safe when multiple parallel instances try to create the directory
// exists and is not a directory -> is a regular file
throw new FileAlreadyExistsException(file.getAbsolutePath());
} else {
File parent = file.getParentFile();
return (parent == null || mkdirsInternal(parent))
&& (file.mkdir() || file.isDirectory());
}
}
@Override
public FSDataOutputStream create(final Path filePath, final WriteMode overwrite)
throws IOException {
checkNotNull(filePath, "filePath");
if (exists(filePath) && overwrite == WriteMode.NO_OVERWRITE) {
throw new FileAlreadyExistsException("File already exists: " + filePath);
}
final Path parent = filePath.getParent();
if (parent != null && !mkdirs(parent)) {
throw new IOException("Mkdirs failed to create " + parent);
}
final File file = pathToFile(filePath);
return new LocalDataOutputStream(file);
}
@Override
public boolean rename(final Path src, final Path dst) throws IOException {
final File srcFile = pathToFile(src);
final File dstFile = pathToFile(dst);
final File dstParent = dstFile.getParentFile();
View on GitHub (pinned to 2f3c205e92)
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.
Example fix
// before fs.create(path, WriteMode.NO_OVERWRITE); // after if (fs.exists(path)) fs.delete(path, true); fs.create(path, WriteMode.NO_OVERWRITE);
Defensive patterns
Strategy: validation
Validate before calling
FSDataOutputStream safeCreate(FileSystem fs, Path p, WriteMode mode) throws IOException {
if (mode == WriteMode.NO_OVERWRITE && fs.exists(p))
throw new FileAlreadyExistsException("will not overwrite: " + p);
return fs.create(p, mode);
} Try / catch
try {
fs.create(p, WriteMode.NO_OVERWRITE);
} catch (FileAlreadyExistsException e) {
fs.delete(p, true);
fs.create(p, WriteMode.NO_OVERWRITE);
} Prevention
- 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.
When it happens
Trigger: Calling create(path, WriteMode.NO_OVERWRITE) on a path where exists(path) is true.
Common situations: 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.
Related errors
- File {} does not exist or the user running Flink ('{}') has
- Directory {} is not empty
- {}
- Mkdirs failed to create {}
- Could not create writer state serializer.
AI-assisted analysis of apache/flink@2f3c205e92 (2026-08-14).
Data as JSON: /api/errors/ed29e94b4f839c59.
Report an issue: GitHub.