karatelabs/karate · warning
Failed to clean output directory:
Error message
Failed to clean output directory:
What it means
Before a `karate run`, the CLI wipes the output directory (target/karate etc.) with deleteDirectory(); if deletion fails it warns 'Failed to clean output directory: <message>' and continues the run instead of aborting. Stale output may therefore mix with fresh reports. The exception is swallowed by design so a locked reports dir doesn't block test execution.
Solutions
- Close viewers/processes holding output files (browser tabs, IDE previews), then rerun
- Delete the output directory manually before running
- Fix permissions on the output directory / run as a user with write access
- In CI, start from a clean workspace (git clean -xdff or fresh checkout)
Example fix
// before karate run # Failed to clean output directory: target/karate (locked by browser) // after (unix) rm -rf ./target/karate && karate run
Defensive patterns
Strategy: validation
Validate before calling
// before karate run (unix)
rm -rf ./target/karate || { echo 'output dir locked'; exit 1; } Try / catch
karate run ... || true; grep 'Failed to clean output directory' run.log && echo 'close browser/IDE holding reports'
Prevention
- Close report HTML in browsers/IDE before rerunning
- Use fresh CI workspaces (git clean -xdff)
- Ensure the run user has write access to the output path
- Delete read-only leftovers proactively (chmod -R u+w target)
When it happens
Trigger: Running `karate run` when the output directory exists but cannot be deleted — report files/HTML open in a browser or IDE, an OS file lock, or permission restrictions on the output path.
Common situations: A browser tab holding karate-report HTML open (common on Windows); a previous run's process still alive; CI workspace with leftover read-only files; running as a service user lacking write access.
Related errors
- Failed to clean:
- Failed to clean: karate-temp
- Failed to backup existing dir
- Failed to close JSONL event stream
- Failed to copy ext assets for
AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12).
Data as JSON: /api/errors/174346a0892d8ac5.
Report an issue: GitHub.
Appendix: source
Thrown at karate-core/src/main/java/io/karatelabs/cli/RunCommand.java:524
public Integer runWithPaths(List<String> legacyPaths) {
this.paths = legacyPaths;
return call();
}
private void cleanOutputDir(String dir, String workDir) {
Path output;
if (workDir != null) {
output = Path.of(workDir).resolve(dir);
} else {
output = Path.of(dir);
}
if (Files.exists(output)) {
try {
deleteDirectory(output.toFile());
Console.println(Console.info("Cleaned: " + output));
} catch (Exception e) {
Console.println(Console.warn("Failed to clean output directory: " + e.getMessage()));
}
}
}
private void deleteDirectory(File dir) {
File[] files = dir.listFiles();
if (files != null) {
for (File file : files) {
if (file.isDirectory()) {
deleteDirectory(file);
} else {
file.delete();
}
}
}
dir.delete();
}
View on GitHub (pinned to a22eb90246)