# Update a variable in an HDF5 file

**URL:** <https://discourse.julialang.org/t/update-a-variable-in-an-hdf5-file/62341>\
**Category:** General Usage\
**Tags:** hdf5\
**Created:** [June 3, 2021, 3:18pm UTC](https://discourse.julialang.org/t/update-a-variable-in-an-hdf5-file/62341 "2021-06-03T15:18:02Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidwallis](https://avatars.discourse-cdn.com/v4/letter/d/87869e/32.png) [@davidwallis](https://discourse.julialang.org/u/davidwallis)\
**Post date:** [June 3, 2021, 3:18pm UTC](https://discourse.julialang.org/t/update-a-variable-in-an-hdf5-file/62341/1 "2021-06-03T15:18:03Z")

</div>

Simple problem, but it’s driving me mad. How do I update a dataset in an HDF5 file?

Thanks

julia\> tp2[“callParams”][“width”]  
🔢 HDF5.Dataset: /callParams/width (file: testproj2.h5 xfer\_mode: 0)

julia\> read(tp2[“callParams/width”])  
128

julia\> tp2[“callParams”][“width”] = 256  
ERROR: cannot create dataset: object “width” already exists at /callParams

julia\> write(tp2,“callParams/width”,256)  
ERROR: cannot create dataset: object “callParams/width” already exists at /

julia\> HDF5.h5open(“testproj.h5”,“r+”) do fid  
cp=fid[“callParams”]  
cp[“width”] = 256  
end  
ERROR: cannot create dataset: object “width” already exists at /callParams

---

<div class="post-metadata">

**Author:** ![shipengcheng1230](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shipengcheng1230/32/4089_2.png) [@shipengcheng1230](https://discourse.julialang.org/u/shipengcheng1230)\
**Post date:** [June 3, 2021, 3:41pm UTC](https://discourse.julialang.org/t/update-a-variable-in-an-hdf5-file/62341/2 "2021-06-03T15:41:45Z")

</div>

Maybe you can check the key:

```julia
julia> file = tempname() * ".h5";

julia> h5write(file, "A/B", 1.1);

julia> h5read(file, "A/B")
1.1

julia> h5open(file, "r+") do f
           if haskey(f, "A/B")
               delete_object(f, "A/B")
           end
           f["A/B"] = 2.2
       end
2.2

```

---

<div class="post-metadata">

**Author:** ![davidwallis](https://avatars.discourse-cdn.com/v4/letter/d/87869e/32.png) [@davidwallis](https://discourse.julialang.org/u/davidwallis)\
**Post date:** [June 3, 2021, 4:28pm UTC](https://discourse.julialang.org/t/update-a-variable-in-an-hdf5-file/62341/3 "2021-06-03T16:28:49Z")

</div>

Yes. That works. Thank you.

Surprised there is not an inbuilt way.

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [June 3, 2021, 6:22pm UTC](https://discourse.julialang.org/t/update-a-variable-in-an-hdf5-file/62341/4 "2021-06-03T18:22:43Z")

</div>

```julia
HDF5.h5open(“testproj.h5”,“w”) do fid

```

Would the above work?  
Edit: Sorry, I think you meant to preserve (other) contents of the file, just replace one variable. This will overwrite the file.

---

<div class="post-metadata">

**Author:** ![davidwallis](https://avatars.discourse-cdn.com/v4/letter/d/87869e/32.png) [@davidwallis](https://discourse.julialang.org/u/davidwallis)\
**Post date:** [June 4, 2021, 6:18am UTC](https://discourse.julialang.org/t/update-a-variable-in-an-hdf5-file/62341/5 "2021-06-04T06:18:33Z")

</div>

Yes, and it also refuses to overwrite because the file exists.

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [June 4, 2021, 7:44am UTC](https://discourse.julialang.org/t/update-a-variable-in-an-hdf5-file/62341/6 "2021-06-04T07:44:18Z")

</div>

Be aware that for older versions of HDF5 this didn’t actually free any space (see for example [Reclaim storage space when deleting a large dataset in hdf5 file - HDF5 - HDF Forum](https://forum.hdfgroup.org/t/reclaim-storage-space-when-deleting-a-large-dataset-in-hdf5-file/4626)). One had to explicitly `h5repack` the file afterwards. From the top of my head, I think they changed this in version 1.10.

---

<div class="post-metadata">

**Author:** ![ggggggggg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ggggggggg/32/265_2.png) [@ggggggggg](https://discourse.julialang.org/u/ggggggggg)\
**Post date:** [June 9, 2021, 5:21pm UTC](https://discourse.julialang.org/t/update-a-variable-in-an-hdf5-file/62341/7 "2021-06-09T17:21:01Z")

</div>

FWIW you can do this in `h5py`, I don’t know if it is deleting the object under the hood. If you made it a length 1 dataset you could change the content of the dataset like you can with larger ones.

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [November 30, 2021, 4:37pm UTC](https://discourse.julialang.org/t/update-a-variable-in-an-hdf5-file/62341/8 "2021-11-30T16:37:59Z")

</div>

Oh, I don’t see a `repack` function in HDF5.jl. Can you recommend a way to do this?
