apache/dolphinscheduler · error · RuntimeException

Flink Script file exists in path: %s before creation and can

Error message

Flink Script file exists in path: %s before creation and cannot be deleted

What it means

FileUtils.writeScriptFile() in the Flink SQL task plugin deletes a pre-existing script file at the target path before writing a new one. If Files.delete(path) throws IOException, it throws RuntimeException('Flink Script file exists in path: %s before creation and cannot be deleted'). This prevents stale or undeletable script files from blocking task execution.

Source

Thrown at dolphinscheduler-task-plugin/dolphinscheduler-task-flink/src/main/java/org/apache/dolphinscheduler/plugin/task/flink/FileUtils.java:73

    public static void generateScriptFile(TaskExecutionContext taskExecutionContext, FlinkParameters flinkParameters) {
        String initScriptFilePath = FileUtils.getInitScriptFilePath(taskExecutionContext);
        String scriptFilePath = FileUtils.getScriptFilePath(taskExecutionContext);
        String initOptionsString = StringUtils.join(
                FlinkArgsUtils.buildInitOptionsForSql(flinkParameters),
                FlinkConstants.FLINK_SQL_NEWLINE).concat(FlinkConstants.FLINK_SQL_NEWLINE);
        writeScriptFile(initScriptFilePath, initOptionsString + flinkParameters.getInitScript());
        writeScriptFile(scriptFilePath, flinkParameters.getRawScript());
    }

    private static void writeScriptFile(String scriptFileFullPath, String script) {
        File scriptFile = new File(scriptFileFullPath);
        Path path = scriptFile.toPath();
        if (Files.exists(path)) {
            try {
                Files.delete(path);
            } catch (IOException e) {
                throw new RuntimeException(String
                        .format("Flink Script file exists in path: %s before creation and cannot be deleted", path), e);
            }
        }

        Set<PosixFilePermission> perms = PosixFilePermissions.fromString(RWXR_XR_X);
        FileAttribute<Set<PosixFilePermission>> attr = PosixFilePermissions.asFileAttribute(perms);
        try {
            if (SystemUtils.IS_OS_WINDOWS) {
                Files.createFile(path);
            } else {
                if (!scriptFile.getParentFile().exists()) {
                    scriptFile.getParentFile().mkdirs();
                }
                Files.createFile(path, attr);
            }

            if (StringUtils.isNotEmpty(script)) {
                String replacedScript = script.replaceAll("\\r\\n", "\n");

View on GitHub (pinned to 02eac45a1b)

Solutions

  1. Check ownership/permissions of the existing script file and the parent directory; ensure the worker user can delete it
  2. Manually remove the stale file, then re-run the task
  3. Verify the tenant execution temp directory is writable and not on a read-only mount
  4. Kill processes holding the file open (e.g. lsof) before retrying

Example fix

// before
Files.delete(path);
// after
try {
    Files.delete(path);
} catch (IOException e) {
    log.warn("could not delete stale script {}, attempting overwrite", path);
}
Defensive patterns

Strategy: validation

Validate before calling

Path path = Paths.get(scriptFileFullPath);
if (Files.exists(path)) {
    if (!Files.isWritable(path.getParent())) {
        throw new IllegalStateException("cannot delete/create script: parent not writable " + path.getParent());
    }
    Files.deleteIfExists(path);
}

Try / catch

try {
    task.handle(null);
} catch (RuntimeException e) {
    if (e.getMessage() != null && e.getMessage().contains("cannot be deleted")) {
        Files.deleteIfExists(Paths.get(expectedScriptPath));
    }
    throw e;
}

Prevention

When it happens

Trigger: A file exists at scriptFileFullPath and deletion fails due to file permissions, the file being locked/held by another process, or it being a non-empty directory.

Common situations: Leftover script from a crashed prior run owned by a different user; file held open by a monitoring process; read-only mount; worker running as a user without delete permission on the tenant temp dir.

Understand the failure class

Background: "failed to write file", "Could not save figure", "Error saving remote file" — file write failed: causes and fixes across languages and libraries — this error's family across 38 libraries.

Related errors


AI-assisted analysis of apache/dolphinscheduler@02eac45a1b (2026-09-06). Data as JSON: /api/errors/0b1d3d8a74226380. Report an issue: GitHub.