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

  1. Close viewers/processes holding output files (browser tabs, IDE previews), then rerun
  2. Delete the output directory manually before running
  3. Fix permissions on the output directory / run as a user with write access
  4. 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

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


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)