{"record":{"id":"1e8950a45c24e9da","repo":"gradle/gradle","slug":"create-static-archive-failed-could-not-delete-pre","errorCode":null,"errorMessage":"Create static archive failed: could not delete previous archive","messagePattern":"Create static archive failed: could not delete previous archive","errorType":"exception","errorClass":"GradleException","httpStatus":null,"severity":"error","filePath":"platforms/native/platform-native/src/main/java/org/gradle/nativeplatform/toolchain/internal/gcc/ArStaticLibraryArchiver.java","lineNumber":57,"sourceCode":"public class ArStaticLibraryArchiver extends AbstractCompiler<StaticLibraryArchiverSpec> {\n    public ArStaticLibraryArchiver(BuildOperationExecutor buildOperationExecutor, CommandLineToolInvocationWorker commandLineToolInvocationWorker, CommandLineToolContext invocationContext, WorkerLeaseService workerLeaseService) {\n        super(buildOperationExecutor, commandLineToolInvocationWorker, invocationContext, new ArchiverSpecToArguments(), false, workerLeaseService);\n    }\n\n    @Override\n    public WorkResult execute(final StaticLibraryArchiverSpec spec) {\n        deletePreviousOutput(spec);\n\n        return super.execute(spec);\n    }\n\n    private void deletePreviousOutput(StaticLibraryArchiverSpec spec) {\n        // Need to delete the previous archive, otherwise stale object files will remain\n        if (!spec.getOutputFile().isFile()) {\n            return;\n        }\n        if (!spec.getOutputFile().delete()) {\n            throw new GradleException(\"Create static archive failed: could not delete previous archive\");\n        }\n    }\n\n    @Override\n    protected Action<BuildOperationQueue<CommandLineToolInvocation>> newInvocationAction(final StaticLibraryArchiverSpec spec, List<String> args) {\n        final CommandLineToolInvocation invocation = newInvocation(\n            \"archiving \" + spec.getOutputFile().getName(), spec.getOutputFile().getParentFile(), args, spec.getOperationLogger());\n\n        return new Action<BuildOperationQueue<CommandLineToolInvocation>>() {\n            @Override\n            public void execute(BuildOperationQueue<CommandLineToolInvocation> buildQueue) {\n                buildQueue.setLogLocation(spec.getOperationLogger().getLogLocation());\n                buildQueue.add(invocation);\n            }\n        };\n    }\n\n    @Override","sourceCodeStart":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/gradle/gradle/blob/534f27719b66953f95cc907aae7f2c1b12f5482d/platforms/native/platform-native/src/main/java/org/gradle/nativeplatform/toolchain/internal/gcc/ArStaticLibraryArchiver.java#L39-L75","documentation":"ArStaticLibraryArchiver deletes the previous static archive (.a) before re-archiving so stale object files cannot survive; if File.delete() returns false it throws GradleException 'Create static archive failed: could not delete previous archive'. The old archive is effectively locked or not deletable by the build user.","triggerScenarios":"A stale .a output file exists and delete() fails: on Windows the file is open in another process (antivirus, indexer, archive tool, a still-running compiler); on Linux permissions/ownership deny unlink, or the directory is read-only.","commonSituations":"Windows builds with real-time antivirus scanning build outputs; a previous build's process still holding the file; build directories shared between users/containers with mixed ownership; network file systems.","solutions":["Delete the stale archive manually (or run gradle clean for that module) and re-run the build","Exclude the project and build directories from antivirus/real-time scanning and file indexing on Windows","Fix ownership/permissions of the build output directory so the build user can unlink files (chmod/chown on the build dir)"],"exampleFix":null,"handlingStrategy":"fallback","validationCode":"// Before building, prove the previous archive is deletable\nval archive = layout.buildDirectory.file(\"lib/main.a\").get().asFile\nif (archive.isFile && !archive.delete()) {\n    throw GradleException(\"$archive is locked — close processes holding it or fix permissions, then rebuild\")\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Exclude project and build directories from antivirus real-time scanning on Windows","Run gradle --stop before deep toolchain/permission changes so no daemon keeps handles open","Give the CI user ownership of build directories; avoid sharing them across users or containers"],"tags":["gradle","native","static-library","file-lock","windows"],"backgroundTag":"file-locked-cannot-delete","analyzedSha":"534f27719b66953f95cc907aae7f2c1b12f5482d","analyzedAt":"2026-08-22T08:09:12.375Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}