{"record":{"id":"f76bce4549d54c8d","repo":"TeamNewPipe/NewPipe","slug":"directory-with-the-same-name-found-but-cannot-dele","errorCode":null,"errorMessage":"Directory with the same name found but cannot delete","messagePattern":"Directory with the same name found but cannot delete","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"app/src/main/java/org/schabi/newpipe/streams/io/StoredFileHelper.java","lineNumber":454,"sourceCode":"    private void takePermissionSAF() throws IOException {\n        try {\n            context.getContentResolver().takePersistableUriPermission(docFile.getUri(),\n                    StoredDirectoryHelper.PERMISSION_FLAGS);\n        } catch (final Exception e) {\n            if (docFile.getName() == null) {\n                throw new IOException(e);\n            }\n        }\n    }\n\n    @NonNull\n    private DocumentFile createSAF(@Nullable final Context ctx, final String mime,\n                                   final String filename) throws IOException {\n        DocumentFile res = StoredDirectoryHelper.findFileSAFHelper(ctx, docTree, filename);\n\n        if (res != null && res.exists() && res.isDirectory()) {\n            if (!res.delete()) {\n                throw new IOException(\"Directory with the same name found but cannot delete\");\n            }\n            res = null;\n        }\n\n        if (res == null) {\n            res = this.docTree.createFile(srcType == null ? DEFAULT_MIME : mime, filename);\n            if (res == null) {\n                throw new IOException(\"Cannot create the file\");\n            }\n        }\n\n        return res;\n    }\n\n    private String getLowerCase(final String str) {\n        return str == null ? null : str.toLowerCase();\n    }\n","sourceCodeStart":436,"sourceCodeEnd":472,"githubUrl":"https://github.com/TeamNewPipe/NewPipe/blob/9e8be091560a69f35d44bd252eb00bc7911c977b/app/src/main/java/org/schabi/newpipe/streams/io/StoredFileHelper.java#L436-L472","documentation":"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.","triggerScenarios":"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).","commonSituations":"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).","solutions":["Prompt the user to manually remove or rename the conflicting directory before retrying the download.","Use the 'safe' creation path (StoredFileHelper with safe=true) after ensuring the name is unique, avoiding the delete-directory branch.","Choose a different filename if a same-named directory exists and cannot be removed.","Verify the tree URI still grants write/delete permission."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// Before createSAF, check if a directory with the same name exists:\nDocumentFile existing = StoredDirectoryHelper.findFileSAFHelper(ctx, tree, filename);\nif (existing != null && existing.isDirectory()) {\n    // warn user or pick a different name instead of trying to delete\n    filename = makeUnique(filename);\n}","typeGuard":null,"tryCatchPattern":"try {\n    StoredFileHelper helper = new StoredFileHelper(context, tree, filename, mime, false);\n} catch (IOException e) {\n    if (e.getMessage() != null && e.getMessage().contains(\"Directory with the same name\")) {\n        // prompt user to remove the directory or use a different name\n        promptResolveNameConflict(filename);\n    } else throw e;\n}","preventionTips":["Detect same-named directories before attempting file creation and either rename or prompt.","Avoid using names that could collide with existing folders in the download directory.","Use the 'safe' creation path with a guaranteed-unique name when possible."],"tags":["android-saf","storage","documentfile","file-conflict","io"],"backgroundTag":null,"analyzedSha":"9e8be091560a69f35d44bd252eb00bc7911c977b","analyzedAt":"2026-08-14T00:11:33.519Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}