apache/hadoop · error · IOException

Failed to create temporary file for {}. File {} should not

Error message

Failed to create temporary file for {}.  File {} should not be present, but is.

What it means

IOException from DatanodeUtil.createFileWithExistsCheck: the DataNode is about to create the zero-length temporary file for a block being written, but a pre-check finds the target file already exists on the volume. Block temp files are expected to be unique for a new write; an existing file at that path means a stale leftover (previous write crashed before cleanup) or duplicate allocation of the same block+generation stamp.

Source

Thrown at hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/DatanodeUtil.java:66

   */ 
  static IOException getCauseIfDiskError(IOException ioe) {
    if (ioe.getMessage()!=null && ioe.getMessage().startsWith(DISK_ERROR)) {
      return (IOException)ioe.getCause();
    } else {
      return null;
    }
  }

  /**
   * Create a new file.
   * @throws IOException 
   * if the file already exists or if the file cannot be created.
   */
  public static File createFileWithExistsCheck(
      FsVolumeSpi volume, Block b, File f,
      FileIoProvider fileIoProvider) throws IOException {
    if (fileIoProvider.exists(volume, f)) {
      throw new IOException("Failed to create temporary file for " + b
          + ".  File " + f + " should not be present, but is.");
    }
    // Create the zero-length temp file
    final boolean fileCreated;
    try {
      fileCreated = fileIoProvider.createFile(volume, f);
    } catch (IOException ioe) {
      throw new IOException(DISK_ERROR + "Failed to create " + f, ioe);
    }
    if (!fileCreated) {
      throw new IOException("Failed to create temporary file for " + b
          + ".  File " + f + " should be creatable, but is already present.");
    }
    return f;
  }
  
  /**
   * @return the meta name given the block name and generation stamp.

View on GitHub (pinned to 2add963021)

Solutions

  1. Trigger block recovery: restart the client application or let lease recovery / block recovery run, which finalizes or deletes the orphaned rbw file
  2. Restart the DataNode - startup scanning and the block report reconcile in-progress files against the NameNode
  3. If the block is already abandoned, delete the orphaned temp file by hand (path is printed in the message) once you confirm the block is not in any NN's block map (hdfs fsck)
  4. Run hdfs fsck to confirm no corruption and let re-replication restore redundancy

Example fix

# before: write fails - 'File /data/dfs/current/BP-.../subdir12/blk_1073741825 should not be present, but is.'

# after: verify the block is orphaned, then remove it and retry the write
hdfs fsck / -files -blocks | grep blk_1073741825 || \
  rm /data/dfs/current/BP-*/subdir12/blk_1073741825   # stop DN first
hdfs --daemon start datanode
Defensive patterns

Strategy: validation

Validate before calling

// Before writing a block, confirm no stale temp file occupies the target path
File rbw = new File(blockDir, block.getBlockName() + "__m" + metaSuffix);
if (rbw.exists()) {
  // stale from a crashed writer: trigger/await block recovery instead of writing over it
  throw new IOException("Stale rbw file present, run block recovery: " + rbw);
}

Try / catch

try {
  datanodeUtil.createFileWithExistsCheck(volume, block, file, fileIoProvider);
} catch (IOException e) {
  if (e.getMessage() != null && e.getMessage().contains("should not be present, but is")) {
    // stale temp file from a prior crashed write: coordinate lease/block recovery
    // or remove the orphan after confirming the block is unknown to the NN
  }
}

Prevention

When it happens

Trigger: A client starts writing block b whose rbw temp file path (under current/BP-.../ subdir with in-progress name) already exists: typically after a DataNode crash/kill -9 mid-write so finalizing/cleanup never ran, or after aborted append/recovery sequences that left the temp file behind.

Common situations: DataNode power loss or OOM kill during heavy writes; repeated failed pipelines for the same block; volume state where a previous incarnation of the same block id and generation stamp was not cleaned; rare duplicate block allocation after NN failover.

Related errors


AI-assisted analysis of apache/hadoop@2add963021 (2026-08-22). Data as JSON: /api/errors/2fe2039f6cd1531e. Report an issue: GitHub.