apache/iceberg · warning

Failed to delete staging file: {}

Error message

Failed to delete staging file: {}

What it means

OSSOutputStream writes data to a local staging file before uploading it to OSS via PutObjectRequest. When the stream is closed, cleanUpStagingFiles() deletes the temp file; if File.delete() fails it logs this warning. The upload itself already succeeded (or the close path handled the error), so this is a leftover temp-file cleanup issue — it can consume local disk if it happens repeatedly.

Source

Thrown at aliyun/src/main/java/org/apache/iceberg/aliyun/oss/OSSOutputStream.java:163

    long contentLength = currentStagingFile.length();
    if (contentLength == 0) {
      LOG.debug("Skipping empty upload to OSS");
      return;
    }

    LOG.debug("Uploading {} staged bytes to OSS", contentLength);
    InputStream contentStream = uncheckedInputStream(currentStagingFile);
    ObjectMetadata metadata = new ObjectMetadata();
    metadata.setContentLength(contentLength);

    PutObjectRequest request =
        new PutObjectRequest(uri.bucket(), uri.key(), contentStream, metadata);
    client.putObject(request);
  }

  private void cleanUpStagingFiles() {
    if (!currentStagingFile.delete()) {
      LOG.warn("Failed to delete staging file: {}", currentStagingFile);
    }
  }

  @SuppressWarnings({"checkstyle:NoFinalizer", "Finalize", "deprecation"})
  @Override
  protected void finalize() throws Throwable {
    super.finalize();
    if (!closed) {
      close(); // releasing resources is more important than printing the warning.
      String trace = Joiner.on("\n\t").join(Arrays.copyOfRange(createStack, 1, createStack.length));
      LOG.warn("Unclosed output stream created by:\n\t{}", trace);
    }
  }
}

View on GitHub (pinned to 86d9c8fc54)

Solutions

  1. Check filesystem permissions on the staging/tmp directory and grant the process delete rights.
  2. Verify nothing else (scanner, backup, another task) is holding the staging file open; configure an exclusion.
  3. Point the staging location (java.io.tmpdir / worker scratch dir) at a writable, task-local directory.
  4. Add periodic cleanup of stale staging files from crashed tasks to reclaim disk space.

Example fix

// before (worker config)
# staging inherits /tmp (read-only in container)
// after
export JAVA_TOOL_OPTIONS="-Djava.io.tmpdir=/var/tmp/iceberg-staging" # writable, task-local
Defensive patterns

Strategy: validation

Validate before calling

Path stagingDir = Path.of(System.getProperty("java.io.tmpdir"));
if (!Files.isWritable(stagingDir)) {
  throw new IllegalStateException("Staging dir not writable: " + stagingDir);
}

Try / catch

try (OutputStream out = file.create()) {
  out.write(data);
} catch (Exception e) {
  // close() already attempted staging cleanup; log includes the file path
  LOG.error("OSS write failed; check staging dir perms and disk space", e);
  throw e;
}

Prevention

When it happens

Trigger: close() calls cleanUpStagingFiles() and File.delete() returns false — the staging file is locked by another process, already deleted, or the process lacks delete permission on the working directory (java.io.tmpdir or the configured staging location).

Common situations: Containers with read-only /tmp; antivirus or backup tools holding the file open on Windows; disk-full or permission problems in the temp dir; concurrent threads sharing the same staging file path.

Understand the failure class

Background: "Permission denied" / "Failed to write" file errors: why a library can't write its files to disk (EACCES, EPERM, ENOSPC) and how to fix them — this error's family across 43 libraries.

Related errors


AI-assisted analysis of apache/iceberg@86d9c8fc54 (2026-09-12). Data as JSON: /api/errors/bd7c86cee6c2e53b. Report an issue: GitHub.