{"record":{"id":"174346a0892d8ac5","repo":"karatelabs/karate","slug":"failed-to-clean-output-directory","errorCode":null,"errorMessage":"Failed to clean output directory: ","messagePattern":"Failed to clean output directory: ","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"karate-core/src/main/java/io/karatelabs/cli/RunCommand.java","lineNumber":524,"sourceCode":"    public Integer runWithPaths(List<String> legacyPaths) {\n        this.paths = legacyPaths;\n        return call();\n    }\n\n    private void cleanOutputDir(String dir, String workDir) {\n        Path output;\n        if (workDir != null) {\n            output = Path.of(workDir).resolve(dir);\n        } else {\n            output = Path.of(dir);\n        }\n\n        if (Files.exists(output)) {\n            try {\n                deleteDirectory(output.toFile());\n                Console.println(Console.info(\"Cleaned: \" + output));\n            } catch (Exception e) {\n                Console.println(Console.warn(\"Failed to clean output directory: \" + e.getMessage()));\n            }\n        }\n    }\n\n    private void deleteDirectory(File dir) {\n        File[] files = dir.listFiles();\n        if (files != null) {\n            for (File file : files) {\n                if (file.isDirectory()) {\n                    deleteDirectory(file);\n                } else {\n                    file.delete();\n                }\n            }\n        }\n        dir.delete();\n    }\n","sourceCodeStart":506,"sourceCodeEnd":542,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/cli/RunCommand.java#L506-L542","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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)"],"exampleFix":"// before\nkarate run  # Failed to clean output directory: target/karate (locked by browser)\n// after (unix)\nrm -rf ./target/karate && karate run","handlingStrategy":"validation","validationCode":"// before karate run (unix)\nrm -rf ./target/karate || { echo 'output dir locked'; exit 1; }","typeGuard":null,"tryCatchPattern":"karate run ... || true; grep 'Failed to clean output directory' run.log && echo 'close browser/IDE holding reports'","preventionTips":["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)"],"tags":["cli","filesystem","output-directory","cleanup"],"backgroundTag":"file-delete-failed","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-19T12:17:13.211Z"}