Repository navigation
Remove a folder in one request, and only an empty one without recursive - #453
Draft
shcheklein wants to merge 7 commits into
Draft
shcheklein wants to merge 7 commits into
shcheklein wants to merge 7 commits into
Conversation
- fs.rm(path, recursive=True) on a folder lists what is in it once and then trashes or deletes the folder with one request, instead of one request per file and folder. It raises PermissionError and removes nothing when this account cannot remove some of the contents. - fs.rm(folder) and fs.rm_file(folder) without recursive removed the folder and everything in it. They now remove it only when it is empty, like rmdir, and raise OSError(ENOTEMPTY) otherwise. - A Drive root alias cannot be removed. - The live test tree is deleted for good at teardown, not trashed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
On real Drive the files in a trashed or deleted folder are trashed or deleted in the background, a few seconds after the folder. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Under a rate limit the filesystem repeats a refused request, which made the request count of the folder removal test one too high. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
shcheklein
commented
Dec 3, 2026
shcheklein
commented
Dec 3, 2026
shcheklein
left a comment
Member
Author
There was a problem hiding this comment.
[AI generated — Copilot]
5-pass review summary: big win over N+1 recursive deletes, with a few real gaps to fix before merging (missing OAuth scope check in _items_that_would_stay, missing Shared Drive driveId guard in rm_file, missing _translate_not_found, unbounded scan/buffering in _items_that_would_stay) plus test teardown/duplication improvements.
This was referenced Dec 3, 2026
- Refuse a Drive root operand before fsspec's rm with maxdepth removes its children, and refuse a Shared Drive root in rm_file too. - Refuse a recursive rm when the scopes can hide children, as rmdir does. - With trash_only=False, also check items already in the trash. - Stop at the first item that would stay and name it. - Raise FileNotFoundError and forget the item when another client removed it, in rm_file and rm as in rmdir. - Skip items inside removed folders with a set, not a scan. - Live test: build the tree under the module folder, include a trashed file, and wait for all files under one deadline. Delete the module folder by ID. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
main renamed _gdrive_retry_read to _gdrive_retry_repeatable in #448. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
🟡 Changes recommended
Root-matching globs can permanently delete children before root protection rejects the operation, because expanded targets are not validated before deletion.
3 open issues
What changed in this PR
Updates filesystem folder removal to prevent accidental recursive deletion and reduce recursive removals to one write request.
Changes:
- Requires folders to be empty for non-recursive removal.
- Adds permission, scope, and Drive-root checks before recursive removal.
- Expands tests and documentation, and permanently deletes live-test fixtures.
| File | Description |
|---|---|
| pydrive2/test/test_fs.py | Adds live removal checks and permanent fixture cleanup. |
| pydrive2/test/test_fs_unit.py | Tests removal safeguards and updates mocked requests. |
| pydrive2/test/test_fs_registration.py | Updates file-ID removal expectations. |
| pydrive2/fs/spec.py | Implements guarded folder removal and shared error handling. |
| docs/fsspec.rst | Documents removal behavior and limitations. |
Review effort: Balanced
- A glob can expand to a root, so rm checks every expanded item for a Drive root before it removes anything, also with maxdepth. - Accept os.PathLike operands, as fsspec's expand_path does. - A recursive rm resolves all targets first and refuses a path that two folders share, as rmdir does. Skipping by path would otherwise skip items of the folder that stays. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


Two changes to how the filesystem removes folders: a recursive removal is now one request instead of one per item, and a removal without
recursiveno longer deletes a folder that has something in it.1.
rm(folder)withoutrecursiveremoved everything in itBefore: the folder and everything under it went to the trash. With
trash_only=Falsethey were deleted for good. fsspec turns a non-recursivermintorm_file(folder), andrm_fileremoved any item it was given. Local filesystems refuse here.After, both
fs.rm(folder)andfs.rm_file(folder)remove a folder only when it is empty, likermdir. Otherwise they raise:2.
rm(folder, recursive=True)sent one request per itemBefore: 2,014 requests, a lookup and a removal for every file and folder. A failure part way left a half-removed tree.
After: one listing of the tree, then one request that trashes or deletes the folder. Drive removes everything in it. For a tree of 1,000 files in one folder that is about 4 requests.
Drive leaves behind anything this account cannot remove, such as a file someone else owns in a shared folder. So the filesystem checks first: the listing asks Drive whether each item can be trashed (or deleted, with
trash_only=False). At the first one that cannot, it raises and removes nothing:The check follows the rules of
rmdir's emptiness check:trash_only=Falseit also lists items already in the trash, because deleting the folder deletes them too.drive.file, or scopes that are unknown), because the listing could then look complete when it is not.rmdir.A Drive root (
root,appDataFolder, or a Shared Drive's ID) cannot be removed. ForrootandappDataFoldernamed outright, the check is made before any request. That includesrmwithmaxdepth, which would otherwise remove the root's children before refusing the root. A Shared Drive root is refused after its metadata is read. A glob can expand to a root too (rm("root/**", recursive=True, maxdepth=1)), sormchecks every expanded item before it removes any.A recursive
rmresolves all its targets before removing any. Likermdir, it raisesFileExistsErrorfor a path that two folders share, instead of removing the one with the lower ID: the items of the other one would otherwise be skipped as if they were removed with it.If another client removes the item between the lookup and the removal,
rmandrm_fileraiseFileNotFoundError, asrmdirdoes.rmwithmaxdepthkeeps fsspec's item-by-item removal. With the first change, a folder deeper thanmaxdepthis no longer removed with its contents.Validation
Live, on real Drive:
rm(folder, recursive=True)on a folder with a file, a subfolder holding a file, and a file already in the trash, once in trash mode and once withtrash_only=False:rm([dup, dup/b.txt], recursive=True)where two folders are nameddupraisesFileExistsError, and both folders' files stay.rm(folder)withoutrecursiveon a folder with files raisesENOTEMPTY, and the files stay.rm(folder)on an empty folder removes it.Offline, for what one service account cannot produce on real Drive:
PermissionErrornaming the item, no write request, and no request for the next page of the listing; withtrash_only=Falsethe listing includes trashed items;PermissionErrorbefore any listing;PermissionErrorbefore any listing;rm(with and withoutrecursive, withmaxdepth, in a list, as aPurePosixPath) andrm_fileofrootorappDataFolder:PermissionErrorbefore any request; of a Shared Drive root: after one metadata read;rm("root/**", recursive=True, maxdepth=1):PermissionErrorand only reads;FileNotFoundError, and the next lookup asks Drive again.Nine offline tests used
rm_fileon folders. They now get the extra emptiness check, or callrm(..., recursive=True). The offline test of the old item-by-item request pattern is replaced by the live request count.Breaking each new check (the contents check, the folder's own permission, the root refusal, the emptiness check) fails a test.
The module fixture of the live filesystem tests now deletes its folder for good, with one request. Before, every CI job left its whole test tree in the trash.
The fsspec guide's "Removing folders" section describes the rules. The offline suite passes with the current google-auth and with 2.15.0 (548 passed).
🤖 Generated with Claude Code