apache/hadoop · error · InconsistentFSStateException

Can't format the storage directory because the current direc

Error message

Can't format the storage directory because the current directory is not empty.

What it means

Storage.analyzeStorage treats a storage directory as 'having current' only when current/VERSION exists (Storage.java:716-717). During FORMAT (or HOTSWAP on a fresh directory) with checkCurrentIsEmpty, checkEmptyCurrent() opens a directory stream on current/ and throws InconsistentFSStateException if it yields any entry. The guard fires when current/ contains leftover files but has no VERSION file — format refuses to silently destroy data it cannot identify.

Source

Thrown at hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/common/Storage.java:622

    /**
     * Check to see if current/ directory is empty. This method is used
     * before determining to format the directory.
     *
     * @throws InconsistentFSStateException if not empty.
     * @throws IOException if unable to list files under the directory.
     */
    private void checkEmptyCurrent() throws InconsistentFSStateException,
        IOException {
      File currentDir = getCurrentDir();
      if(currentDir == null || !currentDir.exists()) {
        // if current/ does not exist, it's safe to format it.
        return;
      }
      try(DirectoryStream<java.nio.file.Path> dirStream =
          Files.newDirectoryStream(currentDir.toPath())) {
        if (dirStream.iterator().hasNext()) {
          throw new InconsistentFSStateException(root,
              "Can't format the storage directory because the current "
                  + "directory is not empty.");
        }
      }
    }

    /**
     * Check consistency of the storage directory.
     *
     * @param startOpt a startup option.
     * @param storage The Storage object that manages this StorageDirectory.
     *
     * @return state {@link StorageState} of the storage directory
     * @throws InconsistentFSStateException if directory state is not
     * consistent and cannot be recovered.
     * @throws IOException
     */
    public StorageState analyzeStorage(StartupOption startOpt, Storage storage)

View on GitHub (pinned to 2add963021)

Solutions

  1. If the contents are disposable: 'hdfs namenode -format -force' to proceed over the non-empty directory
  2. Or move/delete the stray current/ contents manually (back them up first), then re-run plain format
  3. If the files are meaningful (partial namespace), recover them before any destructive step — this exception is an intentional data-loss guard

Example fix

# before
sudo -u hdfs hdfs namenode -format   # InconsistentFSStateException: current not empty

# after (data loss accepted)
sudo -u hdfs hdfs namenode -format -force
Defensive patterns

Strategy: validation

Validate before calling

File current = new File(sd.getRoot(), Storage.STORAGE_DIR_CURRENT);
File version = new File(current, Storage.STORAGE_FILE_VERSION /* VERSION */);
if (current.exists() && current.list().length > 0 && !version.exists()) {
  // leftover data without VERSION: decide explicitly
  if (!force) throw new IOException("current/ non-empty and VERSION missing; "
      + "back up, then format with -force or clean manually");
}

Try / catch

try {
  storage.format(...);
} catch (InconsistentFSStateException e) {
  if (e.getMessage().contains("current directory is not empty")) {
    promptOperator("Storage has unidentifiable data in current/; "
        + "back it up, then rerun with -force to format");
  } else throw e;
}

Prevention

When it happens

Trigger: 'hdfs namenode -format' (without -force) on a storage directory where current/ exists and is non-empty but VERSION is missing: a previously crashed format, a hand-copied/partial directory, or an operator-deleted VERSION file.

Common situations: Format interrupted midway leaving stray files; storage directory reused from another purpose; VERSION accidentally deleted; restoring a partial backup that omitted VERSION.

Related errors


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