{"record":{"id":"16cf1833a9b28687","repo":"apache/hadoop","slug":"destination-path-s-already-exist-cannot-rename","errorCode":null,"errorMessage":"Destination path %s already exist, cannot rename!","messagePattern":"Destination path (.+?) already exist, cannot rename!","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/sftp/SFTPFileSystem.java","lineNumber":479,"sourceCode":"   * @return rename successful?\n   * @throws IOException\n   */\n  private boolean rename(ChannelSftp channel, Path src, Path dst)\n      throws IOException {\n    Path workDir;\n    try {\n      workDir = new Path(channel.pwd());\n    } catch (SftpException e) {\n      throw new IOException(e);\n    }\n    Path absoluteSrc = makeAbsolute(workDir, src);\n    Path absoluteDst = makeAbsolute(workDir, dst);\n\n    if (!exists(channel, absoluteSrc)) {\n      throw new IOException(String.format(E_SPATH_NOTEXIST, src));\n    }\n    if (exists(channel, absoluteDst)) {\n      throw new IOException(String.format(E_DPATH_EXIST, dst));\n    }\n    boolean renamed = true;\n    try {\n      final String previousCwd = channel.pwd();\n      channel.cd(\"/\");\n      channel.rename(src.toUri().getPath(), dst.toUri().getPath());\n      channel.cd(previousCwd);\n    } catch (SftpException e) {\n      renamed = false;\n    }\n    return renamed;\n  }\n\n  @Override\n  public void initialize(URI uriInfo, Configuration conf) throws IOException {\n    super.initialize(uriInfo, conf);\n\n    setConfigurationFromURI(uriInfo, conf);","sourceCodeStart":461,"sourceCodeEnd":497,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/sftp/SFTPFileSystem.java#L461-L497","documentation":"Thrown by SFTPFileSystem.rename when the destination path already exists on the SFTP server (exists(channel, absoluteDst) is true). The adapter refuses overwrite-by-rename: unlike POSIX rename(2) and unlike HDFS (which returns false), SFTPFileSystem raises an IOException with the destination path in the message.","triggerScenarios":"fs.rename(src, dst) where dst names an existing file or an existing directory on the SFTP server; also retry loops that re-run a 'move to archive' step after a previous attempt already created the destination.","commonSituations":"Re-running an idempotent ingestion job that moves files into an archive directory that already holds a file with the same name; renaming onto an existing directory; porting scripts from a filesystem where rename silently replaces the destination.","solutions":["Delete the destination before renaming: if (fs.exists(dst)) { fs.delete(dst, false); } (use recursive=true only if replacing a directory is intended).","Generate unique destination names (append a timestamp or UUID) when the rename may run more than once.","For idempotent pipelines, catch the IOException and treat 'Destination path ... already exist' as 'move already completed' rather than a failure."],"exampleFix":"// before\nfs.rename(src, dst); // throws if dst exists\n\n// after\nif (fs.exists(dst)) {\n  fs.delete(dst, false);\n}\nfs.rename(src, dst);","handlingStrategy":"validation","validationCode":"if (fs.exists(dst)) {\n  fs.delete(dst, false); // deliberate clobber; use true only to replace a directory\n}\nboolean ok = fs.rename(src, dst);","typeGuard":null,"tryCatchPattern":"try {\n  fs.rename(src, dst);\n} catch (IOException e) {\n  if (e.getMessage() != null && e.getMessage().contains(\"already exist\")) {\n    // destination present: idempotent no-op\n  } else {\n    throw e;\n  }\n}","preventionTips":["Make rename jobs idempotent: unique destination names or a pre-rename delete.","Never rely on POSIX overwrite-by-rename semantics over sftp://.","Log the destination path on failure so reruns can reconcile state."],"tags":["sftp","rename","destination-exists","hadoop","filesystem"],"backgroundTag":"destination-already-exists","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}