{"record":{"id":"d5785b61a76c67a3","repo":"apache/hadoop","slug":"can-t-format-the-storage-directory-because-the-cur","errorCode":null,"errorMessage":"Can't format the storage directory because the current directory is not empty.","messagePattern":"Can't format the storage directory because the current directory is not empty\\.","errorType":"exception","errorClass":"InconsistentFSStateException","httpStatus":null,"severity":"error","filePath":"hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/common/Storage.java","lineNumber":622,"sourceCode":"\n    /**\n     * Check to see if current/ directory is empty. This method is used\n     * before determining to format the directory.\n     *\n     * @throws InconsistentFSStateException if not empty.\n     * @throws IOException if unable to list files under the directory.\n     */\n    private void checkEmptyCurrent() throws InconsistentFSStateException,\n        IOException {\n      File currentDir = getCurrentDir();\n      if(currentDir == null || !currentDir.exists()) {\n        // if current/ does not exist, it's safe to format it.\n        return;\n      }\n      try(DirectoryStream<java.nio.file.Path> dirStream =\n          Files.newDirectoryStream(currentDir.toPath())) {\n        if (dirStream.iterator().hasNext()) {\n          throw new InconsistentFSStateException(root,\n              \"Can't format the storage directory because the current \"\n                  + \"directory is not empty.\");\n        }\n      }\n    }\n\n    /**\n     * Check consistency of the storage directory.\n     *\n     * @param startOpt a startup option.\n     * @param storage The Storage object that manages this StorageDirectory.\n     *\n     * @return state {@link StorageState} of the storage directory\n     * @throws InconsistentFSStateException if directory state is not\n     * consistent and cannot be recovered.\n     * @throws IOException\n     */\n    public StorageState analyzeStorage(StartupOption startOpt, Storage storage)","sourceCodeStart":604,"sourceCodeEnd":640,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/common/Storage.java#L604-L640","documentation":"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.","triggerScenarios":"'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.","commonSituations":"Format interrupted midway leaving stray files; storage directory reused from another purpose; VERSION accidentally deleted; restoring a partial backup that omitted VERSION.","solutions":["If the contents are disposable: 'hdfs namenode -format -force' to proceed over the non-empty directory","Or move/delete the stray current/ contents manually (back them up first), then re-run plain format","If the files are meaningful (partial namespace), recover them before any destructive step — this exception is an intentional data-loss guard"],"exampleFix":"# before\nsudo -u hdfs hdfs namenode -format   # InconsistentFSStateException: current not empty\n\n# after (data loss accepted)\nsudo -u hdfs hdfs namenode -format -force","handlingStrategy":"validation","validationCode":"File current = new File(sd.getRoot(), Storage.STORAGE_DIR_CURRENT);\nFile version = new File(current, Storage.STORAGE_FILE_VERSION /* VERSION */);\nif (current.exists() && current.list().length > 0 && !version.exists()) {\n  // leftover data without VERSION: decide explicitly\n  if (!force) throw new IOException(\"current/ non-empty and VERSION missing; \"\n      + \"back up, then format with -force or clean manually\");\n}","typeGuard":null,"tryCatchPattern":"try {\n  storage.format(...);\n} catch (InconsistentFSStateException e) {\n  if (e.getMessage().contains(\"current directory is not empty\")) {\n    promptOperator(\"Storage has unidentifiable data in current/; \"\n        + \"back it up, then rerun with -force to format\");\n  } else throw e;\n}","preventionTips":["Treat 'current/ exists but no VERSION' as suspect data: quarantine it before formatting","Use -force only after confirming the leftover contents are disposable","Never delete VERSION by hand; interrupted formats should be rerun or fully cleaned"],"tags":["hdfs","storage","format","version-file","data-safety","inconsistent-state"],"backgroundTag":"directory-not-empty","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-23T01:17:44.959Z"}