TeamNewPipe/NewPipe · error · IOException

Directory with the same name found but cannot delete

Error message

Directory with the same name found but cannot delete

What it means

Thrown inside createSAF() (the conflict-resolving file-creation path). The method first calls findFileSAFHelper to detect an existing entry with the target name; if that entry is a directory, it attempts res.delete(). If delete() returns false — the DocumentsProvider refused or failed to delete the directory — the method throws. This guards against silently creating a file alongside an undeletable same-named directory, which would cause later ambiguity.

Source

Thrown at app/src/main/java/org/schabi/newpipe/streams/io/StoredFileHelper.java:454

    private void takePermissionSAF() throws IOException {
        try {
            context.getContentResolver().takePersistableUriPermission(docFile.getUri(),
                    StoredDirectoryHelper.PERMISSION_FLAGS);
        } catch (final Exception e) {
            if (docFile.getName() == null) {
                throw new IOException(e);
            }
        }
    }

    @NonNull
    private DocumentFile createSAF(@Nullable final Context ctx, final String mime,
                                   final String filename) throws IOException {
        DocumentFile res = StoredDirectoryHelper.findFileSAFHelper(ctx, docTree, filename);

        if (res != null && res.exists() && res.isDirectory()) {
            if (!res.delete()) {
                throw new IOException("Directory with the same name found but cannot delete");
            }
            res = null;
        }

        if (res == null) {
            res = this.docTree.createFile(srcType == null ? DEFAULT_MIME : mime, filename);
            if (res == null) {
                throw new IOException("Cannot create the file");
            }
        }

        return res;
    }

    private String getLowerCase(final String str) {
        return str == null ? null : str.toLowerCase();
    }

View on GitHub (pinned to 9e8be09156)

Solutions

  1. Prompt the user to manually remove or rename the conflicting directory before retrying the download.
  2. Use the 'safe' creation path (StoredFileHelper with safe=true) after ensuring the name is unique, avoiding the delete-directory branch.
  3. Choose a different filename if a same-named directory exists and cannot be removed.
  4. Verify the tree URI still grants write/delete permission.
Defensive patterns

Strategy: validation

Validate before calling

// Before createSAF, check if a directory with the same name exists:
DocumentFile existing = StoredDirectoryHelper.findFileSAFHelper(ctx, tree, filename);
if (existing != null && existing.isDirectory()) {
    // warn user or pick a different name instead of trying to delete
    filename = makeUnique(filename);
}

Try / catch

try {
    StoredFileHelper helper = new StoredFileHelper(context, tree, filename, mime, false);
} catch (IOException e) {
    if (e.getMessage() != null && e.getMessage().contains("Directory with the same name")) {
        // prompt user to remove the directory or use a different name
        promptResolveNameConflict(filename);
    } else throw e;
}

Prevention

When it happens

Trigger: createSAF() finds an existing directory named identically to the requested filename; res.isDirectory() is true; res.delete() returns false. Happens when the SAF provider does not allow deleting that directory (permission, non-empty directory on a provider that requires recursive delete, read-only mount, or provider bug).

Common situations: A user previously created a folder named 'video.mp4'; a cloud provider that cannot delete non-empty directories; an SD card mounted read-only; a provider that returns false from delete() for directories even when empty (provider implementation quirk).

Related errors


AI-assisted analysis of TeamNewPipe/NewPipe@9e8be09156 (2026-08-14). Data as JSON: /api/errors/f76bce4549d54c8d. Report an issue: GitHub.