spring-projects/spring-boot · error · ReportableException

Failed to delete existing file

Error message

Failed to delete existing file {}

What it means

Thrown by ProjectGenerator.writeProject when --force was given but outputFile.delete() returned false - the existing output file could not be removed. The OS-level delete failed due to permissions, a lock, or the path being a non-empty directory.

Solutions

  1. Close any program holding the file open (editor, IDE, file manager)
  2. Check and fix filesystem permissions on the file and parent directory
  3. Ensure --output names a file, not a non-empty directory
  4. Delete the file manually, then re-run
Defensive patterns

Strategy: validation

Validate before calling

// Before overwriting, confirm the file is actually deletable.
java.io.File out = new java.io.File(System.getProperty("user.dir"), request.getOutput());
if (out.exists()) {
    if (!out.canWrite()) {
        throw new IllegalStateException("No write permission on " + out);
    }
    if (out.isDirectory() && out.list().length > 0) {
        throw new IllegalStateException("--output is a non-empty directory: " + out);
    }
}

Try / catch

try {
    generator.generateProject(request, force);
} catch (ReportableException ex) {
    if (ex.getMessage().startsWith("Failed to delete existing file")) {
        Log.error("Could not delete existing output - close editors and check permissions: "
            + ex.getMessage());
    }
    throw ex;
}

Prevention

When it happens

Trigger: Overwrite mode on, but the target file is locked by another process, read-only, or the path names a non-empty directory; File.delete() returns false.

Common situations: The output file is open in another application (IDE, editor, file manager); the user lacks write/delete permission; --output accidentally names a directory.

Related errors


AI-assisted analysis of spring-projects/spring-boot@270dfe353f (2026-08-11). Data as JSON: /api/errors/28c90410c7ed4e5c. Report an issue: GitHub.

Appendix: source

Thrown at cli/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/ProjectGenerator.java:148

			}
			else {
				file.mkdir();
			}
			zipStream.closeEntry();
			entry = zipStream.getNextEntry();
		}
	}

	private void writeProject(ProjectGenerationResponse entity, String output, boolean overwrite) throws IOException {
		File outputFile = new File(System.getProperty("user.dir"), output);
		if (outputFile.exists()) {
			if (!overwrite) {
				throw new ReportableException(
						"File '" + outputFile.getName() + "' already exists. Use --force if you want to "
								+ "overwrite or specify an alternate location.");
			}
			if (!outputFile.delete()) {
				throw new ReportableException("Failed to delete existing file " + outputFile.getPath());
			}
		}
		byte[] content = entity.getContent();
		Assert.state(content != null, "'content' must not be null");
		FileCopyUtils.copy(content, outputFile);
		Log.info("Content saved to '" + output + "'");
	}

	private void fixExecutableFlag(File dir, String fileName) {
		File f = new File(dir, fileName);
		if (f.exists()) {
			f.setExecutable(true, false);
		}
	}

}

View on GitHub (pinned to 270dfe353f)