Skip to content

DataTree.to_zarr should be able to overwrite (and delete) zarr groups when mode='w' #10433

Open
@TomNicholas

Description

@TomNicholas

What is your issue?

In looking for a way to delete a single group from a zarr store, I found this somewhat counterintuitive behaviour:

# create a zarr store with two empty groups
store = zarr.storage.MemoryStore()
dt = xr.DataTree.from_dict(
    {'foo': xr.Dataset(), 'bar': xr.Dataset()}
)
dt.to_zarr(store, consolidated=False, zarr_format=3)

# prove that it has two groups
root = zarr.open_group(store, path='/', mode='r')
root.members()
# (('foo', <Group memory://139839489276800/foo>),
#  ('bar', <Group memory://139839489276800/bar>))

# overwrite with only one group
dt_without_group = xr.DataTree.from_dict(
    {'bar': xr.Dataset()}
)
dt_without_group.to_zarr(store2, consolidated=False, zarr_format=3, mode='w')

# but this has not changed anything
root.members()
# (('foo', <Group memory://139839489276800/foo>),
# ('bar', <Group memory://139839489276800/bar>))

I expected mode='w' to overwrite the whole store, which in this case would have effectively deleted the 'bar' group.

The behaviour actually seen is more like what I would have expected to occur with mode='a'. (mode='r+' should error.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugtopic-DataTreeRelated to the implementation of a DataTree classtopic-zarrRelated to zarr storage library

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions