Skip to content

Remove a folder in one request, and only an empty one without recursive - #453

Draft
shcheklein wants to merge 7 commits into
mainfrom
rm-folder-in-one-request
Draft

shcheklein wants to merge 7 commits into
mainfrom
rm-folder-in-one-request

Conversation

@shcheklein

@shcheklein shcheklein commented Dec 3, 2026 •

Copy link
Copy Markdown
Member

Two changes to how the filesystem removes folders: a recursive removal is now one request instead of one per item, and a removal without recursive no longer deletes a folder that has something in it.

1. rm(folder) without recursive removed everything in it

fs.rm("root/experiments")        # forgot recursive=True

Before: the folder and everything under it went to the trash. With trash_only=False they were deleted for good. fsspec turns a non-recursive rm into rm_file(folder), and rm_file removed any item it was given. Local filesystems refuse here.

After, both fs.rm(folder) and fs.rm_file(folder) remove a folder only when it is empty, like rmdir. Otherwise they raise:

OSError: [Errno 66] Directory not empty: 'root/experiments'

2. rm(folder, recursive=True) sent one request per item

fs.rm("root/old-runs", recursive=True)   # 1,000 files

Before: 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:

PermissionError: [Errno 13] Nothing was removed: this account cannot have root/old-runs/shared/theirs.csv moved to the trash: 'root/old-runs'

The check follows the rules of rmdir's emptiness check:

  • With trash_only=False it also lists items already in the trash, because deleting the folder deletes them too.
  • It refuses when the credentials' scopes can hide items (drive.file, or scopes that are unknown), because the listing could then look complete when it is not.
  • Drive checks and removes in separate requests, so the guide asks to coordinate concurrent writers, as for rmdir.

A Drive root (root, appDataFolder, or a Shared Drive's ID) cannot be removed. For root and appDataFolder named outright, the check is made before any request. That includes rm with maxdepth, 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)), so rm checks every expanded item before it removes any.

A recursive rm resolves all its targets before removing any. Like rmdir, it raises FileExistsError for 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, rm and rm_file raise FileNotFoundError, as rmdir does.

rm with maxdepth keeps fsspec's item-by-item removal. With the first change, a folder deeper than maxdepth is 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 with trash_only=False:
    • the client sends one write request and at most 5 reads;
    • Drive then reports every item as trashed, or gone (404) in the permanent mode, the trashed file included. Drive does this for the folder's contents in the background, so the test waits up to three minutes for them. The first CI run checked at once and found the files still in place, while the folder itself was already gone.
  • rm([dup, dup/b.txt], recursive=True) where two folders are named dup raises FileExistsError, and both folders' files stay.
  • rm(folder) without recursive on a folder with files raises ENOTEMPTY, and the files stay.
  • rm(folder) on an empty folder removes it.

Offline, for what one service account cannot produce on real Drive:

  • a folder holding an item this account cannot remove: PermissionError naming the item, no write request, and no request for the next page of the listing; with trash_only=False the listing includes trashed items;
  • a folder this account cannot trash: PermissionError before any listing;
  • credentials whose scopes can hide items: PermissionError before any listing;
  • rm (with and without recursive, with maxdepth, in a list, as a PurePosixPath) and rm_file of root or appDataFolder: PermissionError before any request; of a Shared Drive root: after one metadata read;
  • rm("root/**", recursive=True, maxdepth=1): PermissionError and only reads;
  • a file or folder that another client removed: FileNotFoundError, and the next lookup asks Drive again.

Nine offline tests used rm_file on folders. They now get the extra emptiness check, or call rm(..., 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

shcheklein and others added 4 commits December 2, 2026 19:21
- 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>
Comment thread pydrive2/fs/spec.py Resolved
Comment thread pydrive2/fs/spec.py Resolved
Comment thread pydrive2/fs/spec.py Resolved
Comment thread pydrive2/fs/spec.py Resolved
Comment thread pydrive2/test/test_fs.py Resolved

@shcheklein shcheklein left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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.

Comment thread pydrive2/fs/spec.py Resolved
Comment thread pydrive2/fs/spec.py Resolved
Comment thread pydrive2/fs/spec.py Resolved
Comment thread pydrive2/fs/spec.py Resolved
Comment thread docs/fsspec.rst Resolved
Comment thread pydrive2/test/test_fs.py Resolved
Comment thread pydrive2/test/test_fs.py Resolved
Comment thread pydrive2/test/test_fs.py Resolved
Comment thread pydrive2/test/test_fs_unit.py Resolved
shcheklein and others added 2 commits December 3, 2026 11:06
- 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>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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

Comment thread pydrive2/fs/spec.py Resolved
Comment thread pydrive2/fs/spec.py Resolved
Comment thread pydrive2/fs/spec.py Resolved
- 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>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The review identifies no active issues requiring changes to the folder-removal update.

No open issues 🎉

3 resolved since last review

Review effort: Balanced

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants