{"record":{"id":"95886172a132c635","repo":"apache/hadoop","slug":"cannot-remove-current-directory-curdir","errorCode":null,"errorMessage":"Cannot remove current directory: {curDir}","messagePattern":"Cannot remove current directory: (.+?)","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/common/Storage.java","lineNumber":444,"sourceCode":"     * This does not fully format storage directory. \n     * It cannot write the version file since it should be written last after  \n     * all other storage type dependent files are written.\n     * Derived storage is responsible for setting specific storage values and\n     * writing the version file to disk.\n     * \n     * @throws IOException\n     */\n    public void clearDirectory() throws IOException {\n      File curDir = this.getCurrentDir();\n      if (curDir == null) {\n        // if the directory is null, there is nothing to do.\n        return;\n      }\n      if (curDir.exists()) {\n        File[] files = FileUtil.listFiles(curDir);\n        LOG.info(\"Will remove files: {}\", Arrays.toString(files));\n        if (!(FileUtil.fullyDelete(curDir)))\n          throw new IOException(\"Cannot remove current directory: \" + curDir);\n      }\n      if (!curDir.mkdirs()) {\n        throw new IOException(\"Cannot create directory \" + curDir);\n      }\n      if (permission != null) {\n        try {\n          Set<PosixFilePermission> permissions =\n              PosixFilePermissions.fromString(permission.toString());\n          Files.setPosixFilePermissions(curDir.toPath(), permissions);\n        } catch (UnsupportedOperationException uoe) {\n          // Default to FileUtil for non posix file systems\n          FileUtil.setPermission(curDir, permission);\n        }\n      }\n    }\n\n    /**\n     * Directory {@code current} contains latest files defining","sourceCodeStart":426,"sourceCodeEnd":462,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/common/Storage.java#L426-L462","documentation":"Storage.StorageDirectory.clearDirectory() wipes and recreates current/ during format, rollback and finalize transitions: it lists the files, calls FileUtil.fullyDelete(curDir), and if the recursive delete fails it throws IOException('Cannot remove current directory: <curDir>'). The listed files are logged ('Will remove files: ...') just before, which is the fastest way to see what could not be deleted.","triggerScenarios":"Running 'hdfs namenode -format' (or DataNode initialization/rollback/finalize) while the process cannot delete everything under current/: another process still holds a file (in_use.lock, replica files) via an open handle, permissions/ownership changed, or the storage filesystem is read-only or erroring (NFS stale handles, disk fault).","commonSituations":"Formatting without stopping the old NameNode/DataNode; running the daemon under a different user than the storage dir's owner; storage on NFS with stale file handles; a filesystem remounted read-only after errors.","solutions":["Stop the daemon that owns the storage directory and confirm nothing holds files open (lsof +<pid> or check in_use.lock removed)","Fix ownership/permissions: chown -R <hdfs-user> <storage-dir> and ensure write access","Repair the filesystem layer: remount read-write mounts, resolve NFS stale handles, check dmesg/RAID health","As a last resort, delete current/ manually (after backing up) and re-run format"],"exampleFix":"# before: format fails deleting current/\nsudo -u hdfs hdfs namenode -format   # old NN still running\n\n# after\nhadoop-daemon.sh stop namenode\nlsof +D /dfs/nn/current || true\nsudo chown -R hdfs:hdfs /dfs/nn\nsudo -u hdfs hdfs namenode -format","handlingStrategy":"try-catch","validationCode":"File curDir = sd.getCurrentDir();\nif (curDir != null && curDir.exists()) {\n  if (!Files.isWritable(curDir.getParentFile().toPath())) {\n    throw new IOException(\"Storage parent not writable: \" + curDir.getParent());\n  }\n  try (Stream<Path> s = Files.list(curDir.toPath())) {\n    Path lock = curDir.toPath().resolve(\"in_use.lock\");\n    if (Files.exists(lock)) throw new IOException(\"Lock present; daemon still running?\");\n  }\n}\nsd.clearDirectory();","typeGuard":null,"tryCatchPattern":"try {\n  sd.clearDirectory();\n} catch (IOException e) {\n  if (e.getMessage().startsWith(\"Cannot remove current directory\")) {\n    throw new IOException(\"Stop the owning daemon and fix permissions on \"\n        + sd.getCurrentDir(), e); // actionable rethrow\n  } else throw e;\n}","preventionTips":["Always stop the owning daemon before formatting or rolling back a storage directory","Keep storage directories owned by the daemon user and check with lsof before destructive ops","Avoid NFS for NN/DN storage; check mount health (read-only flags, dmesg) before maintenance"],"tags":["hdfs","storage","format","io","permissions","file-lock"],"backgroundTag":"cannot-delete-directory","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}