java-native-access/jna · error · IOException

The following files could not be trashed: " + failed

Error message

The following files could not be trashed: " + failed

What it means

moveToTrash throws IOException listing the files whose rename into the trash directory failed. Rename failures typically mean permission problems, cross-filesystem moves, or a target name collision the OS refuses to overwrite.

Source

Thrown at contrib/platform/src/com/sun/jna/platform/FileUtils.java:116

        /** The default implementation attempts to move the file to
         * the desktop "Trash" folder.
         */
        @Override
        public void moveToTrash(File... files) throws IOException {
            File trash = getTrashDirectory();
            if (!trash.exists()) {
                throw new IOException("No trash location found (define fileutils.trash to be the path to the trash)");
            }
            List<File> failed = new ArrayList<>();
            for (int i=0;i < files.length;i++) {
                File src = files[i];
                File target = new File(trash, src.getName());
                if (!src.renameTo(target)) {
                    failed.add(src);
                }
            }
            if (failed.size() > 0) {
                throw new IOException("The following files could not be trashed: " + failed);
            }
        }
    }
}

View on GitHub (pinned to d036ad9781)

Solutions

  1. Check logs/failed list — ensure the trash directory is on the same filesystem as the files
  2. Fix permissions on source files and the trash directory
  3. Manually copy-then-delete when rename fails across filesystems

Example fix

// before
FileUtils.getInstance().moveToTrash(files);
// after
try {
    FileUtils.getInstance().moveToTrash(files);
} catch (IOException e) {
    for (File f : files) {
        if (f.renameTo(new File(trashDir, f.getName()))) f.delete();
    }
}
Defensive patterns

Strategy: try-catch

Validate before calling

for (File f : files) {
    if (!f.canRead() || !f.canWrite()) throw new IOException("Cannot move " + f);
}

Try / catch

try {
    FileUtils.getInstance().moveToTrash(files);
} catch (IOException e) {
    // retry via copy+delete for cross-filesystem failures
}

Prevention

When it happens

Trigger: Calling moveToTrash(files...) where src.renameTo(target) returns false for one or more files — e.g. trash on a different filesystem, read-only source/target, or insufficient permissions.

Common situations: Moving files from /tmp into a trash under /home (different mount points, rename fails); files locked or owned by another user; trash directory not writable.

Understand the failure class

Background: "failed to write file", "Could not save figure", "Error saving remote file" — file write failed: causes and fixes across languages and libraries — this error's family across 38 libraries.

Related errors


AI-assisted analysis of java-native-access/jna@d036ad9781 (2026-09-12). Data as JSON: /api/errors/a4ff4dcaa1d42d98. Report an issue: GitHub.