# Updating Type without losing data

**URL:** <https://forum.dfinity.org/t/updating-type-without-losing-data/22602>\
**Category:** Language Support\
**Created:** [September 6, 2023, 3:54pm UTC](https://forum.dfinity.org/t/updating-type-without-losing-data/22602 "2023-09-06T15:54:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jamesbeadle](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jamesbeadle/32/10110_2.png) [@jamesbeadle](https://forum.dfinity.org/u/jamesbeadle)\
**Post date:** [September 6, 2023, 3:54pm UTC](https://forum.dfinity.org/t/updating-type-without-losing-data/22602/1 "2023-09-06T15:54:19Z")

</div>

Good afternoon, I need to add a property to my fantasy teams type and I don’t want to lose all my data (again).

Is there a documented procedure I can follow to add a property and ensure nothing is lost in the pre and post upgrade process?

Any help would be greatly appreciated.

James

---

<div class="post-metadata">

**Author:** ![jamesbeadle](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jamesbeadle/32/10110_2.png) [@jamesbeadle](https://forum.dfinity.org/u/jamesbeadle)\
**Post date:** [September 7, 2023, 8:58am UTC](https://forum.dfinity.org/t/updating-type-without-losing-data/22602/2 "2023-09-07T08:58:30Z")

</div>

Instead of trying to upgrade and keep the data, should I just back it up and recreate it after it’s deleted?

---

<div class="post-metadata">

**Author:** ![h1teshtr1path1](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/h1teshtr1path1/32/24662_2.png) [@h1teshtr1path1](https://forum.dfinity.org/u/h1teshtr1path1)\
**Post date:** [September 7, 2023, 9:57am UTC](https://forum.dfinity.org/t/updating-type-without-losing-data/22602/3 "2023-09-07T09:57:01Z")

</div>

I would say yes. There is no specific way defined for this. I usually do this by backing up data in stable variable and then reassigning the values from back up data to original structures.

---

<div class="post-metadata">

**Author:** ![jamesbeadle](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jamesbeadle/32/10110_2.png) [@jamesbeadle](https://forum.dfinity.org/u/jamesbeadle)\
**Post date:** [September 7, 2023, 9:58am UTC](https://forum.dfinity.org/t/updating-type-without-losing-data/22602/4 "2023-09-07T09:58:25Z")

</div>

Thank you for confirming sir.

---

<div class="post-metadata">

**Author:** ![josephgranata](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/josephgranata/32/21930_2.png) [@josephgranata](https://forum.dfinity.org/u/josephgranata)\
**Post date:** [September 7, 2023, 6:49pm UTC](https://forum.dfinity.org/t/updating-type-without-losing-data/22602/5 "2023-09-07T18:49:37Z")

</div>

@jamesbeadle,

I am guessing you are using Motoko, and in that case you are stuck with pre-upgrade, and post-upgrade methods to ensure you keep the data.

Rust has two libraries that make you able to persist data without pre-and-post upgrade calls, but the learning curve to master Rust on the IC is much higher than the one for Motoko.

So, I have some good news for you that may be useful there is a library that can persist data even if you use Motoko, if you haven’t see it is called Motoko-Hash-Map by @ZhenyaUsenko:

> **[GitHub - ZhenyaUsenko/motoko-hash-map: Stable hash maps for Motoko](https://github.com/ZhenyaUsenko/motoko-hash-map)**
>
> Stable hash maps for Motoko. Contribute to ZhenyaUsenko/motoko-hash-map development by creating an account on GitHub.

It works very well, and I do recommend it.

Let me know about your fantasy game, sounds fun!

Joseph

---

<div class="post-metadata">

**Author:** ![Ori](https://avatars.discourse-cdn.com/v4/letter/o/ecae2f/32.png) [@Ori](https://forum.dfinity.org/u/Ori)\
**Post date:** [September 7, 2023, 7:19pm UTC](https://forum.dfinity.org/t/updating-type-without-losing-data/22602/6 "2023-09-07T19:19:20Z")

</div>

If you’re changing the types and they’re not backward compatible (you can use didc to compare the old and new candid, see here: [https://internetcomputer.org/docs/current/motoko/main/upgrades/#upgrade-safety](https://internetcomputer.org/docs/current/motoko/main/upgrades/#upgrade-safety) ), then you can migrate the old data when reloading it in postupgrade:

Write a function that maps the old type to new and pass the data through this in postupgrade. Then remove this function for future upgrades since you wouldn’t need it again.
