apache/hadoop · error · PathIsDirectoryException

Is a directory

Error message

Is a directory

What it means

PathIsDirectoryException ('Is a directory') thrown by Rm.processPath (Delete.java:113) when 'hadoop fs -rm' (without -r/-R) targets an existing directory. The shell deliberately mirrors POSIX rm: deleting a directory requires the recursive flag, which sets deleteDirs=true and skips this guard.

Source

Thrown at hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/shell/Delete.java:113

        return super.expandArgument(arg);
      } catch (PathNotFoundException e) {
        if (!ignoreFNF) {
          throw e;
        }
        // prevent -f on a non-existent glob from failing
        return Collections.emptyList();
      }
    }

    @Override
    protected void processNonexistentPath(PathData item) throws IOException {
      if (!ignoreFNF) super.processNonexistentPath(item);
    }

    @Override
    protected void processPath(PathData item) throws IOException {
      if (item.stat.isDirectory() && !deleteDirs) {
        throw new PathIsDirectoryException(item.toString());
      }

      // TODO: if the user wants the trash to be used but there is any
      // problem (ie. creating the trash dir, moving the item to be deleted,
      // etc), then the path will just be deleted because moveToTrash returns
      // false and it falls thru to fs.delete.  this doesn't seem right
      if (moveToTrash(item) || !canBeSafelyDeleted(item)) {
        return;
      }
      if (!item.fs.delete(item.path, deleteDirs)) {
        throw new PathIOException(item.toString());
      }
      out.println("Deleted " + item);
    }

    private boolean canBeSafelyDeleted(PathData item)
        throws IOException {
      boolean shouldDelete = true;

View on GitHub (pinned to 2add963021)

Solutions

  1. If recursive delete is intended: 'hadoop fs -rm -r /data/somedir'
  2. If only files should go: expand to files explicitly, e.g. 'hadoop fs -rm "/data/somedir/*"'
  3. Pre-check with 'hadoop fs -test -d /path' in scripts and branch to -rm -r
  4. In the Java API use fs.delete(path, true) for directories

Example fix

# before
hadoop fs -rm /data/somedir     # Is a directory

# after
hadoop fs -rm -r /data/somedir
Defensive patterns

Strategy: validation

Validate before calling

FileStatus st = fs.getFileStatus(path);
boolean recursive = st.isDirectory();
fs.delete(path, recursive);

Type guard

static boolean isDirectory(FileSystem fs, Path p) throws IOException {
  return fs.getFileStatus(p).isDirectory();
}

Try / catch

try {
  fs.delete(path, false);
} catch (PathIsDirectoryException e) {
  fs.delete(path, true); // escalate to recursive only if intended
}

Prevention

When it happens

Trigger: 'hadoop fs -rm /data/somedir' where the path is a directory; scripts that delete a path assuming it is a file but the path is (or has become) a parent directory; output dirs left by a previous job.

Common situations: Re-running a cleanup step after a job recreated a directory; parameterizing 'hadoop fs -rm $PATH' where $PATH sometimes points at a directory; porting 'rm -rf' habits without the -r flag.

Related errors


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