Skip to content

obstore delete_dir #3310

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/3310.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add obstore implementation of delete_dir.
10 changes: 10 additions & 0 deletions src/zarr/storage/_obstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
Store,
SuffixByteRequest,
)
from zarr.core.common import concurrent_map
from zarr.core.config import config

if TYPE_CHECKING:
Expand Down Expand Up @@ -196,6 +197,15 @@ async def delete(self, key: str) -> None:
with contextlib.suppress(FileNotFoundError):
await obs.delete_async(self.store, key)

async def delete_dir(self, prefix: str) -> None:
# docstring inherited
self._check_writable()
if prefix != "" and not prefix.endswith("/"):
prefix += "/"

keys = [(k,) async for k in self.list_prefix(prefix)]
Copy link
Contributor

Choose a reason for hiding this comment

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

If you're not using the async iterator from the list, you could also just call metas = await obs.list(self.store, prefix=prefix).collect_async() and then extract the path from each dict. That might give a tiny bit less async overhead

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks for the suggestion, seems good to me.

await concurrent_map(keys, self.delete, limit=config.get("async.concurrency"))

@property
def supports_partial_writes(self) -> bool:
# docstring inherited
Expand Down
11 changes: 11 additions & 0 deletions tests/test_store/test_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,17 @@ async def test_store_getsize_prefix(self, store: ObjectStore) -> None:
total_size = await store.getsize_prefix("c")
assert total_size == len(buf) * 2

async def test_store_delete(self, store: ObjectStore) -> None:
assert store.supports_deletes
buf = cpu.Buffer.from_bytes(b"\x01\x02\x03\x04")
await store.set("foo/1", buf)
await store.set("foo/2", buf)
await store.delete("foo/1")
assert not await store.exists("foo/1")
assert await store.exists("foo/2")
await store.delete_dir("foo") # FileNotFoundErrors are suppressed
assert not await store.exists("foo/2")


@pytest.mark.slow_hypothesis
def test_zarr_hierarchy():
Expand Down
Loading