{"record":{"id":"718d4bc41f98b2ac","repo":"MyCATApache/Mycat-Server","slug":"failed-to-delete","errorCode":null,"errorMessage":"Failed to delete: ","messagePattern":"Failed to delete: ","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"src/main/java/io/mycat/memory/unsafe/utils/JavaUtils.java","lineNumber":88,"sourceCode":"    if (file.isDirectory() && !isSymlink(file)) {\n      IOException savedIOException = null;\n      for (File child : listFilesSafely(file)) {\n        try {\n          deleteRecursively(child);\n        } catch (IOException e) {\n          // In case of multiple exceptions, only last one will be thrown\n          savedIOException = e;\n        }\n      }\n      if (savedIOException != null) {\n        throw savedIOException;\n      }\n    }\n\n    boolean deleted = file.delete();\n    // Delete can also fail if the file simply did not exist.\n    if (!deleted && file.exists()) {\n      throw new IOException(\"Failed to delete: \" + file.getAbsolutePath());\n    }\n  }\n\n  private static File[] listFilesSafely(File file) throws IOException {\n    if (file.exists()) {\n      File[] files = file.listFiles();\n      if (files == null) {\n        throw new IOException(\"Failed to list files for dir: \" + file);\n      }\n      return files;\n    } else {\n      return new File[0];\n    }\n  }\n\n  private static boolean isSymlink(File file) throws IOException {\n    Preconditions.checkNotNull(file);\n    File fileInCanonicalDir = null;","sourceCodeStart":70,"sourceCodeEnd":106,"githubUrl":"https://github.com/MyCATApache/Mycat-Server/blob/65f8d8beb752f935752f2a0eec0ab017facab9ef/src/main/java/io/mycat/memory/unsafe/utils/JavaUtils.java#L70-L106","documentation":"JavaUtils.deleteRecursively deletes a file or directory tree. It throws IOException when File.delete() fails (returns false) and the file still exists — e.g. due to a lock, permissions, or a non-empty directory handled elsewhere. Note delete() also returns false if the file never existed, which is why the exists() check is included.","triggerScenarios":"Calling deleteRecursively on a path where the OS refuses deletion: read-only directory, file held open by another process (Windows), permission-denied, or an I/O error on the filesystem.","commonSituations":"Cleaning up temp directories while another thread/process still has files open; deleting files owned by another user; running on Windows with mapped/network drives; racing with a concurrent writer recreating files.","solutions":["Close any streams/locks or stop the process holding the file before deleting.","Check and fix filesystem permissions on the file and its parent directory.","Retain/inspect the exception's path to identify the specific file and delete it manually to see the OS error.","Retry deletion after a short delay if a virus scanner or indexer transiently holds the file (common on Windows)."],"exampleFix":"// before\nJavaUtils.deleteRecursively(tempDir);\n// after\ntry {\n  JavaUtils.deleteRecursively(tempDir);\n} catch (IOException e) {\n  logger.warn(\"Could not delete temp dir: \" + e.getMessage());\n}","handlingStrategy":"try-catch","validationCode":"if (file.exists() && !file.canWrite() && file.isDirectory()) {\n  // parent dir not writable — deletion will fail\n}","typeGuard":null,"tryCatchPattern":"try {\n  JavaUtils.deleteRecursively(path);\n} catch (IOException e) {\n  logger.warn(\"Failed to delete {}: {}\", path, e.getMessage());\n}","preventionTips":["Close all streams and file locks before deleting temp directories.","Ensure the process user owns or has write permission on the files and parent dirs.","On Windows, watch for antivirus/indexers holding files; retry after a delay.","Catch and log cleanup failures instead of letting them mask primary errors."],"tags":["java","filesystem","io"],"backgroundTag":"file-write-failed","analyzedSha":"65f8d8beb752f935752f2a0eec0ab017facab9ef","analyzedAt":"2026-09-11T00:12:21.696Z","contentChangedAt":"2026-09-11T00:12:21.696Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}