spring-projects/spring-boot · error · ReportableException

Failed to delete existing file {outputFile.getPath()}

Error message

Failed to delete existing file {outputFile.getPath()}

What it means

In writeProject, after --force was requested, File.delete() returned false for the existing outputFile. The library cannot proceed because the old file blocks the write, so it reports the path that could not be deleted.

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 5b2dbdbb8b)

Solutions

  1. Close any editor/IDE/process holding the file, then retry.
  2. Remove the read-only flag / fix ownership / permissions on the file or its directory, then retry.
  3. If the handle is held by another process you cannot stop, delete the file manually (or specify a different --output).

Example fix

// before
$ spring init --force --output demo.zip   # demo.zip open in IDE
  -> Failed to delete existing file /path/demo.zip
// after
# close IDE, then
$ spring init --force --output demo.zip
Defensive patterns

Strategy: try-catch

Validate before calling

File f = new File(System.getProperty("user.dir"), request.getOutput());
if (f.exists() && !f.delete()) {
    throw new IllegalStateException("Cannot delete " + f + " - close holders, fix permissions, or pick another output.");
}

Type guard

boolean deletable = outputFile.exists() && outputFile.getCanonicalFile().delete();
// note: this is destructive; only call when --force is intended

Try / catch

try { generator.generateProject(request, true); }
catch (ReportableException ex) {
    if (ex.getMessage().startsWith("Failed to delete existing file")) {
        // surface to user: close the file elsewhere / fix perms / choose new output
        System.err.println(ex.getMessage() + " - close any program holding the file and retry.");
    } else throw ex;
}

Prevention

When it happens

Trigger: --force is set, outputFile.exists() is true, and outputFile.delete() returns false. This happens when the file is read-only, locked by another process, or the JVM lacks filesystem permission to delete it (common on Windows where the file is open elsewhere).

Common situations: Windows with the file open in an editor/IDE; file marked read-only or owned by another user; antivirus or indexer holding a handle; network-mounted filesystem with delete restrictions.

Related errors


AI-assisted analysis of spring-projects/spring-boot@5b2dbdbb8b (2026-08-04). Data as JSON: /data/errors/4aba80f745e49d8b.json. Report an issue: GitHub.