apache/hadoop · error · UnsupportedOperationException

{getClass().getSimpleName()} doesn't support truncate

Error message

{getClass().getSimpleName()} doesn't support truncate

What it means

truncate() is an optional AbstractFileSystem operation; the base implementation at AbstractFileSystem.java:756 throws UnsupportedOperationException naming the concrete class. Only file systems that override it support truncation — HDFS does (HDFS-3107), and local does in this version via RawLocalFileSystem.truncate reached through DelegateToFileSystem — but many third-party or custom AbstractFileSystem implementations do not.

Source

Thrown at hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/AbstractFileSystem.java:759

      UnresolvedLinkException, IOException;

  /**
   * The specification of this method matches that of
   * {@link FileContext#truncate(Path, long)} except that Path f must be for
   * this file system.
   *
   * @param f the path.
   * @param newLength new length.
   * @throws AccessControlException access control exception.
   * @throws FileNotFoundException file not found exception.
   * @throws UnresolvedLinkException unresolved link exception.
   * @throws IOException raised on errors performing I/O.
   * @return if successfully truncate success true, not false.
   */
  public boolean truncate(Path f, long newLength)
      throws AccessControlException, FileNotFoundException,
      UnresolvedLinkException, IOException {
    throw new UnsupportedOperationException(getClass().getSimpleName()
        + " doesn't support truncate");
  }

  /**
   * The specification of this method matches that of
   * {@link FileContext#setReplication(Path, short)} except that Path f must be
   * for this file system.
   *
   * @param f the path.
   * @param replication replication.
   * @return if successfully set replication success true, not false.
   * @throws AccessControlException access control exception.
   * @throws FileNotFoundException file not found exception.
   * @throws UnresolvedLinkException unresolved link exception.
   * @throws IOException raised on errors performing I/O.
   */
  public abstract boolean setReplication(final Path f,
      final short replication) throws AccessControlException,

View on GitHub (pinned to 2add963021)

Solutions

  1. Guard with the capability probe: fs.hasPathCapability(path, CommonPathCapabilities.FS_CAPABILITY_PATH_TRUNCATE) before calling truncate
  2. Fallback rewrite: copy the wanted prefix to a temp file, then delete + rename over the original
  3. For your own AbstractFileSystem, implement truncate() (and expose the capability) instead of relying on the base throw

Example fix

// before
fs.truncate(p, newLen); // RawCustomFs doesn't support truncate

// after
if (fs.hasPathCapability(p, CommonPathCapabilities.FS_CAPABILITY_PATH_TRUNCATE)) {
  fs.truncate(p, newLen);
} else {
  rewritePrefix(fs, p, newLen); // copy prefix -> temp, delete, rename
}
Defensive patterns

Strategy: try-catch

Validate before calling

FileSystem fs = ...;
if (fs.hasPathCapability(p, CommonPathCapabilities.FS_CAPABILITY_PATH_TRUNCATE)) {
  fs.truncate(p, newLen);
} else {
  rewritePrefix(fs, p, newLen); // copy prefix to temp, delete, rename
}

Type guard

static boolean supportsTruncate(FileSystem fs, Path p) {
  return fs.hasPathCapability(p, CommonPathCapabilities.FS_CAPABILITY_PATH_TRUNCATE);
}

Try / catch

try { fc.truncate(p, newLen); } catch (UnsupportedOperationException e) { rewritePrefix(fc, p, newLen); }

Prevention

When it happens

Trigger: FileContext.truncate(path, newLength) or direct afs.truncate(...) on a file system whose AbstractFileSystem does not override truncate: custom in-house filesystems, ftp/sftp-backed DelegateToFileSystem whose underlying FileSystem throws, some object-store adapters.

Common situations: Portable code assuming truncate works everywhere because it works on HDFS; the same job running against test/local/HDFS mounts with different capability sets; downstream libraries (compaction, format writers) that truncate tail data as an optimization.

Related errors


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