{"record":{"id":"6c35d1d01ee6da73","repo":"apache/hadoop","slug":"directory-is-not-empty-6c35d1","errorCode":null,"errorMessage":"Directory is not empty","messagePattern":"Directory is not empty","errorType":"exception","errorClass":"PathIsNotEmptyDirectoryException","httpStatus":null,"severity":"error","filePath":"hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/shell/Delete.java","lineNumber":215,"sourceCode":"    @Override\n    protected void processOptions(LinkedList<String> args) throws IOException {\n      CommandFormat cf = new CommandFormat(\n          1, Integer.MAX_VALUE, \"-ignore-fail-on-non-empty\");\n      cf.parse(args);\n      ignoreNonEmpty = cf.getOpt(\"-ignore-fail-on-non-empty\");\n    }\n\n    @Override\n    protected void processPath(PathData item) throws IOException {\n      if (!item.stat.isDirectory()) {\n        throw new PathIsNotDirectoryException(item.toString());\n      }      \n      if (item.fs.listStatus(item.path).length == 0) {\n        if (!item.fs.delete(item.path, false)) {\n          throw new PathIOException(item.toString());\n        }\n      } else if (!ignoreNonEmpty) {\n        throw new PathIsNotEmptyDirectoryException(item.toString());\n      }\n    }\n  }\n\n  // delete files from the trash that are older\n  // than the retention threshold.\n  static class Expunge extends FsCommand {\n    private static final String OPTION_FILESYSTEM = \"fs\";\n\n    public static final String NAME = \"expunge\";\n    public static final String USAGE =\n        \"[-immediate] [-\" + OPTION_FILESYSTEM + \" <path>]\";\n    public static final String DESCRIPTION =\n        \"Delete files from the trash that are older \" +\n            \"than the retention threshold\";\n\n    private boolean emptyImmediately = false;\n    private String fsArgument;","sourceCodeStart":197,"sourceCodeEnd":233,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/shell/Delete.java#L197-L233","documentation":"PathIsNotEmptyDirectoryException ('Directory is not empty') thrown by Rmdir.processPath (Delete.java:215): 'hadoop fs -rmdir' was called on a directory whose listStatus() returned entries, and the -ignore-fail-on-non-empty flag was not set. rmdir intentionally refuses to delete non-empty directories, POSIX style.","triggerScenarios":"'hadoop fs -rmdir /data/dir' when /data/dir still contains files or subdirectories; hidden or zero-length files (e.g. .crc-style or _temporary) left by failed jobs; scripts that rmdir immediately after deleting visible files while background tasks recreate entries.","commonSituations":"Post-job cleanup after a partially failed job leaves _temporary/ or part files; users expecting rmdir to behave like rm -r; testing cleanup logic against directories with dot-files.","solutions":["If recursive removal is fine: 'hadoop fs -rm -r /data/dir'","If you want best-effort rmdir semantics, add the flag: 'hadoop fs -rmdir -ignore-fail-on-non-empty /data/dir'","Empty the directory first ('hadoop fs -rm \"/data/dir/*\"') then rmdir — beware subdirectories need -r too","List the directory ('hdfs dfs -ls -a /data/dir') to find what is keeping it non-empty"],"exampleFix":"# before\nhadoop fs -rmdir /output/job1     # Directory is not empty\n\n# after\nhadoop fs -rm -r /output/job1","handlingStrategy":"validation","validationCode":"if (fs.listStatus(path).length != 0) {\n  // decide: fs.delete(path, true) OR throw PathIsNotEmptyDirectoryException\n}","typeGuard":"static boolean isEmptyDirectory(FileSystem fs, Path p) throws IOException {\n  return fs.getFileStatus(p).isDirectory() && fs.listStatus(p).length == 0;\n}","tryCatchPattern":"try {\n  fs.delete(path, false);\n} catch (PathIsNotEmptyDirectoryException e) {\n  // escalate to recursive delete only if data loss is acceptable\n}","preventionTips":["Use rm -r when the tree must go; rmdir is only for empty dirs","Add -ignore-fail-on-non-empty for best-effort cleanup scripts","List with -a to catch hidden markers before assuming a dir is empty"],"tags":["hadoop","rmdir","shell","directory-not-empty","argument-validation"],"backgroundTag":"directory-not-empty","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}