{"record":{"id":"a09e6a8198ca45e5","repo":"apache/hadoop","slug":"destination-destfile-exists-but-cannot-be-dele","errorCode":null,"errorMessage":"Destination '{destFile}' exists but cannot be deleted","messagePattern":"Destination '(.+?)' exists but cannot be deleted","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/common/Storage.java","lineNumber":1355,"sourceCode":"    }\n    if (srcFile.getCanonicalPath().equals(destFile.getCanonicalPath())) {\n      throw new IOException(\"Source '\" + srcFile + \"' and destination '\" +\n          destFile + \"' are the same\");\n    }\n    File parentFile = destFile.getParentFile();\n    if (parentFile != null) {\n      if (!parentFile.mkdirs() && !parentFile.isDirectory()) {\n        throw new IOException(\"Destination '\" + parentFile\n            + \"' directory cannot be created\");\n      }\n    }\n    if (destFile.exists()) {\n      if (FileUtil.canWrite(destFile) == false) {\n        throw new IOException(\"Destination '\" + destFile\n            + \"' exists but is read-only\");\n      } else {\n        if (destFile.delete() == false) {\n          throw new IOException(\"Destination '\" + destFile\n              + \"' exists but cannot be deleted\");\n        }\n      }\n    }\n    try {\n      NativeIO.copyFileUnbuffered(srcFile, destFile);\n    } catch (NativeIOException e) {\n      throw new IOException(\"Failed to copy \" + srcFile.getCanonicalPath()\n          + \" to \" + destFile.getCanonicalPath()\n          + \" due to failure in NativeIO#copyFileUnbuffered(). \"\n          + e.toString());\n    }\n    if (srcFile.length() != destFile.length()) {\n      throw new IOException(\"Failed to copy full contents from '\" + srcFile\n          + \"' to '\" + destFile + \"'\");\n    }\n    if (preserveFileDate) {\n      if (destFile.setLastModified(srcFile.lastModified()) == false) {","sourceCodeStart":1337,"sourceCodeEnd":1373,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/common/Storage.java#L1337-L1373","documentation":"Guard clause of Storage.nativeCopyFileUnbuffered: destFile exists, passes the canWrite check, but File.delete() still returned false. Deletion can fail even on writable files when the file is held open by another process (notably on Windows), the containing directory's permissions prevent unlink, or SELinux/ACLs deny removal.","triggerScenarios":"Another process holds the destination open (Windows file locking, silly-rename on NFS); the directory has the sticky bit and the daemon user is not the owner; SELinux policy denies unlink; a virus scanner or backup tool has the file open.","commonSituations":"Two daemons or a monitoring tool touching the same storage files; NFS-mounted storage; hardened directories with the sticky bit; antivirus on Windows nodes.","solutions":["Find and stop the process holding the file open (lsof <dest>) and retry","Verify the daemon user owns both the file and its directory (sticky-bit rule) and fix ownership","Check SELinux/audit logs for unlink denials and adjust the policy or contexts"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"if (destFile.exists()) {\n  File parent = destFile.getParentFile();\n  boolean stickySafe = (parent == null) || !stickyBitSet(parent)\n      || destFile.getCanonicalPath().startsWith(System.getProperty(\"user.home\"));\n  if (!destFile.delete()) throw new IOException(\"cannot delete \" + destFile\n      + \"; held open or sticky-bit/ownership conflict\");\n}","typeGuard":null,"tryCatchPattern":"try {\n  Storage.nativeCopyFileUnbuffered(src, dst, true);\n} catch (IOException e) {\n  if (e.getMessage() != null && e.getMessage().contains(\"cannot be deleted\")) {\n    // lsof <dst> to find the holder, stop it, retry once\n  }\n}","preventionTips":["Ensure only one process touches a storage file at a time","Run daemons and cleanup jobs as the owning user","On Windows, close all handles before checkpoint cleanup"],"tags":["hdfs","file-copy","delete","permissions","file-locking"],"backgroundTag":"file-delete-failed","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}