{"record":{"id":"9fc2a240b6d76adf","repo":"HMCL-dev/HMCL","slug":"failed-to-remove-instance-before-restoring-backup","errorCode":null,"errorMessage":"Failed to remove instance before restoring backup: ","messagePattern":"Failed to remove instance before restoring backup: ","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"HMCLCore/src/main/java/org/jackhuang/hmcl/modpack/ModpackUpdateTask.java","lineNumber":95,"sourceCode":"    /// Requests post-execution cleanup or rollback after the update task terminates.\n    ///\n    /// @return `true`\n    @Override\n    public boolean doPostExecute() {\n        return true;\n    }\n\n    /// Retains the backup after success, or restores it and refreshes the repository after failure.\n    @Override\n    public void postExecute() throws Exception {\n        if (isDependenciesSucceeded()) {\n            // Keep backup game version for further repair.\n            return;\n        }\n\n        // Restore backup\n        if (!instance.getRepository().removeInstanceFromDisk(instance.getId())) {\n            throw new IOException(\"Failed to remove instance before restoring backup: \" + instance.getId());\n        }\n\n        FileUtils.copyDirectory(backupFolder, instance.getInstanceRoot());\n        instance.getRepository().refresh();\n    }\n}\n","sourceCodeStart":77,"sourceCodeEnd":102,"githubUrl":"https://github.com/HMCL-dev/HMCL/blob/24702dc5a0214034f4c27166d5fd30cad08cec19/HMCLCore/src/main/java/org/jackhuang/hmcl/modpack/ModpackUpdateTask.java#L77-L102","documentation":"ModpackUpdateTask.postExecute() throws IOException when it cannot delete the existing instance directory from disk before copying the backup over it. Restoring a backup requires a clean instance root; if removeInstanceFromDisk fails, the restore is aborted to avoid mixing old and new files.","triggerScenarios":"Updating/restoring a modpack instance where removeInstanceFromDisk(instance.getId()) returns false - typically because files are locked, read-only, or partially undeletable on the filesystem.","commonSituations":"Windows file locks (game or launcher still holding files in the instance), permission issues on the instance directory, antivirus locking mods. Note the message concatenates the instance ID, so the trailing text shows which instance failed.","solutions":["Close any running Minecraft/launcher processes using the instance, then retry the update","Manually delete the instance directory and retry the restore/update","Check and fix filesystem permissions on the instances folder","Exclude the HMCL instances directory from antivirus real-time scanning"],"exampleFix":"// before (locked by running game)\nrepository.removeInstanceFromDisk(instance.getId()) // false -> IOException\n// after\n// stop the game first, or pre-clean:\nFileUtils.deleteDirectory(instance.getInstanceRoot());","handlingStrategy":"retry","validationCode":"if (Files.exists(instanceRoot) && !isDirectoryWritable(instanceRoot)) { /* abort before update */ }","typeGuard":null,"tryCatchPattern":"try { task.run(); } catch (IOException e) { if (e.getMessage().startsWith(\"Failed to remove instance\")) { /* prompt user to close the game / free the directory, then retry */ } }","preventionTips":["Ensure no game/launcher processes hold files in the instance directory before updating","Exclude instances folder from antivirus locks","Run the launcher with sufficient filesystem permissions"],"tags":["filesystem","io","modpack"],"backgroundTag":"file-delete-failed","analyzedSha":"24702dc5a0214034f4c27166d5fd30cad08cec19","analyzedAt":"2026-09-10T12:36:46.680Z","contentChangedAt":"2026-09-10T12:36:46.680Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}