{"record":{"id":"11740904234102ed","repo":"apache/hadoop","slug":"rename-cannot-overwrite-non-empty-destination-dire-117409","errorCode":null,"errorMessage":"rename cannot overwrite non empty destination directory ${dst}","messagePattern":"rename cannot overwrite non empty destination directory (.+?)","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/FileSystem.java","lineNumber":1706,"sourceCode":"    try {\n      dstStatus = getFileLinkStatus(dst);\n    } catch (IOException e) {\n      dstStatus = null;\n    }\n    if (dstStatus != null) {\n      if (srcStatus.isDirectory() != dstStatus.isDirectory()) {\n        throw new IOException(\"Source \" + src + \" Destination \" + dst\n            + \" both should be either file or directory\");\n      }\n      if (!overwrite) {\n        throw new FileAlreadyExistsException(\"rename destination \" + dst\n            + \" already exists.\");\n      }\n      // Delete the destination that is a file or an empty directory\n      if (dstStatus.isDirectory()) {\n        FileStatus[] list = listStatus(dst);\n        if (list != null && list.length != 0) {\n          throw new IOException(\n              \"rename cannot overwrite non empty destination directory \" + dst);\n        }\n      }\n      delete(dst, false);\n    } else {\n      final Path parent = dst.getParent();\n      final FileStatus parentStatus = getFileStatus(parent);\n      if (parentStatus == null) {\n        throw new FileNotFoundException(\"rename destination parent \" + parent\n            + \" not found.\");\n      }\n      if (!parentStatus.isDirectory()) {\n        throw new ParentNotDirectoryException(\"rename destination parent \" + parent\n            + \" is a file.\");\n      }\n    }\n    if (!rename(src, dst)) {\n      throw new IOException(\"rename from \" + src + \" to \" + dst + \" failed.\");","sourceCodeStart":1688,"sourceCodeEnd":1724,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/FileSystem.java#L1688-L1724","documentation":"With Rename.OVERWRITE, the deprecated rename replaces the destination only via delete(dst, false), which cannot remove a populated directory. If listStatus(dst) is non-empty it throws IOException('rename cannot overwrite non empty destination directory dst'): overwrite is limited to files and empty directories.","triggerScenarios":"rename(dir -> existing non-empty dir, OVERWRITE), e.g. publishing a directory of part-files onto a previous run's populated output directory.","commonSituations":"Users expecting 'mv'-style recursive replacement; staging/commit code renaming whole result trees onto existing trees.","solutions":["Delete the destination tree explicitly first: fs.delete(dst, true), then rename","Or rename INTO the directory (dst = dstDir/srcName) instead of over it","Use per-run destination names and clean old runs separately"],"exampleFix":"// before\nfs.rename(srcDir, dstDir, Rename.OVERWRITE); // dstDir non-empty\n\n// after\nif (fs.exists(dstDir) && fs.getFileStatus(dstDir).isDirectory()) {\n  fs.delete(dstDir, true);\n}\nfs.rename(srcDir, dstDir);","handlingStrategy":"validation","validationCode":"if (fs.exists(dst)) {\n  FileStatus s = fs.getFileStatus(dst);\n  boolean replaceable = !s.isDirectory() || fs.listStatus(dst).length == 0;\n  if (!replaceable) {\n    fs.delete(dst, true); // OVERWRITE can only replace files/empty dirs\n  }\n}\nfs.rename(src, dst, Rename.OVERWRITE);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never assume recursive-overwrite semantics from Rename.OVERWRITE","Clean destination trees as an explicit pipeline step","Prefer renaming into a directory over replacing a directory"],"tags":["hadoop","filesystem","rename","directory-not-empty"],"backgroundTag":"rename-destination-not-empty","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-23T01:17:44.959Z"}