{"record":{"id":"5f68a1b068102a0b","repo":"apache/hadoop","slug":"could-not-rename-olddir-to-newdir","errorCode":null,"errorMessage":"Could not rename {oldDir} to {newDir}","messagePattern":"Could not rename (.+?) to (.+?)","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/MapFile.java","lineNumber":903,"sourceCode":"      }\n      data.close();\n    }\n\n  }\n\n  /**\n   * Renames an existing map directory.\n   * @param fs fs.\n   * @param oldName oldName.\n   * @param newName newName.\n   * @throws IOException raised on errors performing I/O.\n   */\n  public static void rename(FileSystem fs, String oldName, String newName)\n    throws IOException {\n    Path oldDir = new Path(oldName);\n    Path newDir = new Path(newName);\n    if (!fs.rename(oldDir, newDir)) {\n      throw new IOException(\"Could not rename \" + oldDir + \" to \" + newDir);\n    }\n  }\n\n  /**\n   * Deletes the named map file.\n   * @param fs input fs.\n   * @param name input name.\n   * @throws IOException raised on errors performing I/O.\n   */\n  public static void delete(FileSystem fs, String name) throws IOException {\n    Path dir = new Path(name);\n    Path data = new Path(dir, DATA_FILE_NAME);\n    Path index = new Path(dir, INDEX_FILE_NAME);\n\n    fs.delete(data, true);\n    fs.delete(index, true);\n    fs.delete(dir, true);\n  }","sourceCodeStart":885,"sourceCodeEnd":921,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/MapFile.java#L885-L921","documentation":"Thrown by MapFile.rename(FileSystem, String, String) when FileSystem.rename() returns false. A MapFile is a directory containing data and index files, so 'renaming the map' means renaming the whole directory; the underlying FS refused. On HDFS the classic refusal reason is that the destination already exists — its rename is not POSIX-overwrite.","triggerScenarios":"Calling MapFile.rename where the destination directory already exists (HDFS rename does not overwrite); source path missing; user lacks permission on source or parent of destination; destination parent doesn't exist.","commonSituations":"Committing map output by renaming part-r dir onto an existing final dir from a previous run (re-run without cleaning output); atomic-commit patterns ported from local FS assumptions to HDFS; permission changes between runs.","solutions":["Delete or move the existing destination first: if (fs.exists(newDir)) fs.delete(newDir, true) — only if overwrite is intended.","Verify the source exists and you have write permission on both source and destination parent (hadoop fs -ls, -chmod).","Ensure the destination parent directory exists before renaming.","Wrap in try-catch (IOException) and surface both paths (already in the message) for the failure report."],"exampleFix":"// before: second run throws because target exists\nMapFile.rename(fs, \"/out/part-0\", \"/out/final\");\n\n// after: HDFS rename does not overwrite — clear the target explicitly\nPath dst = new Path(\"/out/final\");\nif (fs.exists(dst)) {\n  fs.delete(dst, true);\n}\nMapFile.rename(fs, \"/out/part-0\", dst.toString());","handlingStrategy":"try-catch","validationCode":"Path dst = new Path(newName);\nif (fs.exists(dst)) {\n  if (!overwrite) throw new IOException(dst + \" already exists\");\n  fs.delete(dst, true); // HDFS rename will not overwrite\n}\nMapFile.rename(fs, oldName, newName);","typeGuard":null,"tryCatchPattern":"try {\n  MapFile.rename(fs, oldName, newName);\n} catch (IOException e) {\n  if (e.getMessage() != null && e.getMessage().startsWith(\"Could not rename\")) {\n    // destination exists, source missing, or perms — diagnose both paths\n    if (fs.exists(new Path(newName))) fs.delete(new Path(newName), true);\n    MapFile.rename(fs, oldName, newName); // retry once after clearing target\n  } else { throw e; }\n}","preventionTips":["Treat HDFS rename as no-overwrite: always pre-check fs.exists(dst) and delete deliberately.","Clean job output directories before re-runs so commit-time renames never collide.","Verify write permission on the destination PARENT directory, not just the target."],"tags":["mapfile","rename","hdfs","hadoop-common"],"backgroundTag":"rename-failed","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}