{"record":{"id":"34d87c3c12be665d","repo":"apache/hadoop","slug":"rename-destination-dst-already-exists-34d87c","errorCode":null,"errorMessage":"rename destination ${dst} already exists.","messagePattern":"rename destination (.+?) already exists\\.","errorType":"exception","errorClass":"FileAlreadyExistsException","httpStatus":null,"severity":"error","filePath":"hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/FileSystem.java","lineNumber":1699,"sourceCode":"        if (option == Rename.OVERWRITE) {\n          overwrite = true;\n        }\n      }\n    }\n\n    FileStatus dstStatus;\n    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      }","sourceCodeStart":1681,"sourceCodeEnd":1717,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/FileSystem.java#L1681-L1717","documentation":"In the deprecated options-aware rename: the destination exists and Rename.OVERWRITE was not among the options, so the default implementation refuses with FileAlreadyExistsException('rename destination dst already exists.') - POSIX-style protection against silently clobbering an existing target.","triggerScenarios":"rename(src, dst, ...) without Rename.OVERWRITE while dst exists; publish/commit flows re-running against a previous run's output.","commonSituations":"Idempotent pipeline reruns; atomic-publish patterns (rename temp -> final) where the final path already exists from an earlier successful run.","solutions":["If overwriting is intended, pass Rename.OVERWRITE","Treat FileAlreadyExistsException as 'already published' and skip (idempotent commit)","Otherwise move or delete the existing destination first"],"exampleFix":"// before\nfs.rename(src, dst); // dst exists -> FileAlreadyExistsException\n\n// after\ntry {\n  fs.rename(src, dst, Rename.OVERWRITE);\n} catch (FileAlreadyExistsException e) {\n  LOG.info(\"already published: {}\", dst); // idempotent rerun\n}","handlingStrategy":"validation","validationCode":"if (fs.exists(dst)) {\n  if (!overwrite) {\n    LOG.info(\"destination already exists, treating as done: {}\", dst);\n    return;\n  }\n}\nfs.rename(src, dst, overwrite ? new Rename[]{Rename.OVERWRITE} : new Rename[0]);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Pass OVERWRITE only where data loss is acceptable","Make commit steps idempotent by expecting already-exists outcomes","Use unique per-run temp names and a final publish rename"],"tags":["hadoop","filesystem","rename","file-exists"],"backgroundTag":"destination-already-exists","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}