{"record":{"id":"eda0e6a8abead52c","repo":"alibaba/nacos","slug":"delete-fail","errorCode":null,"errorMessage":"delete fail","messagePattern":"delete fail","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"warning","filePath":"common/src/main/java/com/alibaba/nacos/common/utils/IoUtils.java","lineNumber":237,"sourceCode":"     * <p>If is dir, clean directory, do not delete dir.\n     *\n     * <p>If is file, delete file.\n     *\n     * @param fileOrDir file or dir\n     * @throws IOException io exception\n     */\n    public static void delete(File fileOrDir) throws IOException {\n        if (fileOrDir == null) {\n            return;\n        }\n        \n        if (fileOrDir.isDirectory()) {\n            cleanDirectory(fileOrDir);\n        } else {\n            if (fileOrDir.exists()) {\n                boolean isDeleteOk = fileOrDir.delete();\n                if (!isDeleteOk) {\n                    throw new IOException(\"delete fail\");\n                }\n            }\n        }\n    }\n    \n    /**\n     * 清理目录下的内容. Clean content under directory.\n     *\n     * @param directory directory\n     * @throws IOException io exception\n     */\n    public static void cleanDirectory(File directory) throws IOException {\n        if (!directory.exists()) {\n            String message = directory + \" does not exist\";\n            throw new IllegalArgumentException(message);\n        }\n        \n        if (!directory.isDirectory()) {","sourceCodeStart":219,"sourceCodeEnd":255,"githubUrl":"https://github.com/alibaba/nacos/blob/9b989acdf181d00898f2e8839257bb2b2a3cefe3/common/src/main/java/com/alibaba/nacos/common/utils/IoUtils.java#L219-L255","documentation":"Thrown by IoUtils.delete(File) when File.delete() returns false for an existing regular file. delete() returning false (rather than throwing) is the JDK's signal that deletion did not succeed — typically because the file is locked, in use, or the caller lacks filesystem permission. The IOException carries no path detail, so you must know which file you passed.","triggerScenarios":"Deleting a file that is still open by a stream/channel, locked by another process, on a read-only filesystem, or where the JVM lacks delete permission.","commonSituations":"Windows where open file handles block deletion; deleting a config/log file still being written; container read-only volume; concurrent process holding the file.","solutions":["Ensure all streams/channels to the file are closed (try-with-resources) before deleting.","On Windows, retry deletion after a short delay once handles are released, or call System.gc() as a last resort to release mapped buffers.","Verify the path is on a writable filesystem and the process has delete permission.","Replace IoUtils.delete with Files.deleteIfExists + explicit attribute handling if you need richer diagnostics."],"exampleFix":"// before\nIoUtils.delete(file); // throws IOException(\"delete fail\") if locked\n\n// after\ntry { IoUtils.delete(file); }\ncatch (IOException e) {\n    if (!file.exists()) return; // already gone\n    // close any open handles, then retry with NIO\n    Files.deleteIfExists(file.toPath());\n}","handlingStrategy":"try-catch","validationCode":"// Ensure no open handles remain before deleting.\n// (streams/channels must be closed via try-with-resources upstream)\nif (fileOrDir != null && fileOrDir.exists() && !fileOrDir.isDirectory()) {\n    // safe to attempt delete\n}","typeGuard":null,"tryCatchPattern":"try {\n    IoUtils.delete(file);\n} catch (IOException e) {\n    if (!file.exists()) return; // already removed\n    try { Files.deleteIfExists(file.toPath()); }\n    catch (IOException ex) { throw new IOException(\"Cannot delete \" + file, ex); }\n}","preventionTips":["Close all streams/channels (try-with-resources) before deleting a file.","On Windows, expect locked-file failures and retry or force GC of mapped buffers.","Verify writable filesystem and delete permissions.","Use Files.deleteIfExists for better diagnostics than the bare 'delete fail'."],"tags":["io","filesystem","file-lock","windows","permissions"],"backgroundTag":null,"analyzedSha":"9b989acdf181d00898f2e8839257bb2b2a3cefe3","analyzedAt":"2026-08-14T07:17:31.569Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}