Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions changes/2451.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Make `LocalStore.delete` and `delete_sync` remove only the specified object key.
Directory paths are now a no-op and retain their children, matching `MemoryStore`.
Use `delete_dir` to remove a directory and all keys below it.
10 changes: 5 additions & 5 deletions src/zarr/storage/_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,8 @@ def _clear(root: Path) -> None:


def _delete(path: Path) -> None:
if path.is_dir():
shutil.rmtree(path)
else:
# Directories represent key prefixes, not stored objects.
if not path.is_dir():
path.unlink(missing_ok=True)


Expand Down Expand Up @@ -401,8 +400,9 @@ async def delete(self, key: str) -> None:

Notes
-----
If ``key`` is a directory within this store, the entire directory
at ``store.root / key`` is deleted.
If ``key`` is a directory within this store, nothing is deleted:
directories represent prefixes rather than stored objects. Use
`delete_dir` to remove all keys under a directory.
"""
# docstring inherited
self._check_writable()
Expand Down
2 changes: 1 addition & 1 deletion tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -907,7 +907,7 @@ async def test_open_with_mode_a(tmp_path: Path) -> None:
# Open without shape argument should default to group
g = zarr.open(store=tmp_path, mode="a")
assert isinstance(g, Group)
await g.store_path.delete()
await g.store_path.delete_dir()

# 'a' means read/write (create if doesn't exist)
arr = zarr.open(store=tmp_path, mode="a", shape=(3, 3))
Expand Down
58 changes: 45 additions & 13 deletions tests/test_store/test_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,19 +242,51 @@ async def test_empty_with_empty_subdir(self, store: LocalStore) -> None:
(store.root / "foo/bar").mkdir(parents=True)
assert await store.is_empty("")

def test_delete_sync_directory(self, store: LocalStore) -> None:
"""`delete_sync` on a key that is a directory must remove the whole tree.

Mirrors the async `delete_dir` behavior: deleting `"foo"` where
`"foo"` is a directory containing further nested paths should remove
everything under it, not just fail or delete a single file.
"""
(store.root / "foo" / "bar").mkdir(parents=True)
(store.root / "foo" / "bar" / "baz").write_bytes(b"data")

store.delete_sync("foo")

assert not (store.root / "foo").exists()
@pytest.mark.parametrize("key", ["foo", "foo/", "foo/bar", ""])
@pytest.mark.parametrize("synchronous", [False, True])
async def test_delete_directory_keeps_keys(
self, store: LocalStore, key: str, synchronous: bool
) -> None:
"""Directories are not object keys; deleting one must preserve its children."""
values = {"foo/bar/baz": b"nested", "foobar/other": b"sibling", "file": b"file"}
for name, value in values.items():
await store.set(name, self.buffer_cls.from_bytes(value))
assert not await store.exists(key)
if synchronous:
store.delete_sync(key)
else:
await store.delete(key)
assert sorted(await _collect(store.list())) == sorted(values)
for name, value in values.items():
assert_bytes_equal(await store.get(name), self.buffer_cls.from_bytes(value))

@pytest.mark.parametrize("synchronous", [False, True])
async def test_delete_file_keeps_siblings(self, store: LocalStore, synchronous: bool) -> None:
value = self.buffer_cls.from_bytes(b"data")
await store.set("foo/a", value)
await store.set("foo/b", value)
for key in ("foo/a", "foo/a", "missing"):
if synchronous:
store.delete_sync(key)
else:
await store.delete(key)
assert sorted(await _collect(store.list())) == ["foo/b"]
assert_bytes_equal(await store.get("foo/b"), value)
await store.delete_dir("foo")
assert await _collect(store.list()) == []

@pytest.mark.parametrize("synchronous", [False, True])
async def test_delete_directory_read_only(self, store: LocalStore, synchronous: bool) -> None:
value = self.buffer_cls.from_bytes(b"data")
await store.set("foo/a", value)
read_only = store.with_read_only(True)
if synchronous:
with pytest.raises(ValueError, match="read-only"):
read_only.delete_sync("foo")
else:
with pytest.raises(ValueError, match="read-only"):
await read_only.delete("foo")
assert_bytes_equal(await store.get("foo/a"), value)

def test_creates_new_directory(self, tmp_path: pathlib.Path) -> None:
target = tmp_path.joinpath("a", "b", "c")
Expand Down
Loading