Deleting hdf5 dataset using h5py

Yes, this can be done.

with h5py.File(input,  "a") as f:
    del f[datasetname]

You will need to have the file open in a writeable mode, for example append (as above) or write.

As noted by @seppo-enarvi in the comments the purpose of the previously recommended f.__delitem__(datasetname) function is to implement the del operator, so that one can delete a dataset using del f[datasetname]


I tried this out and the only way I could actually reduce the size of the file is by copying everything to a new file and just leaving out the dataset I was not interested in:

fs = h5py.File('WFA.h5', 'r')
fd = h5py.File('WFA_red.h5', 'w')
for a in fs.attrs:
    fd.attrs[a] = fs.attrs[a]
for d in fs:
    if not 'SFS_TRANSITION' in d: fs.copy(d, fd)