# Stable variables persistence

**URL:** <https://forum.dfinity.org/t/stable-variables-persistence/34039>\
**Category:** Language Support\
**Created:** [August 9, 2024, 7:23am UTC](https://forum.dfinity.org/t/stable-variables-persistence/34039 "2024-08-09T07:23:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jaxopaxo](https://avatars.discourse-cdn.com/v4/letter/j/b5ac83/32.png) [@jaxopaxo](https://forum.dfinity.org/u/jaxopaxo)\
**Post date:** [August 9, 2024, 7:23am UTC](https://forum.dfinity.org/t/stable-variables-persistence/34039/1 "2024-08-09T07:23:15Z")

</div>

I have something always worrying me about data persistence, I know stable variables keeps the data permanent, but what is someone changes the model accidentally, then the data is lost for ever in this case. My suggestion is that when model changes in stable variables, the code should not compile, and to delete a stable variable, there should be two ways, 1- By adding a feature to DFX to delete the variable, and issue for example dfx canister stable delete canister\_id.stablevariable  
then dfx deploy, the dfx canister delete would only add a flag to dfx to continue build as usual, and if that can be anoying we should add a flag to dfx deploy like dfx deploy --ignore-stable-error  
Then it would go ahead and delete the stable variable/s without any errors

2- Issuing delete command in code and deploy for example

as usual Create a new stable variable, and copy the data from MODEL\_OLD to MODEL\_NEW  
add new keyword to delete the variable → delete … MODEL\_OLD;

---

<div class="post-metadata">

**Author:** ![luc-blaeser](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/luc-blaeser/32/7410_2.png) [@luc-blaeser](https://forum.dfinity.org/u/luc-blaeser)\
**Post date:** [August 12, 2024, 8:00am UTC](https://forum.dfinity.org/t/stable-variables-persistence/34039/2 "2024-08-12T08:00:10Z")

</div>

Hi @jaxopaxo,  
Thank you for reaching out. I completely understand your worries about accidental deletion of data. However, `dfx` should warn (even with an error message) when stable variables in Motoko are deleted. Interactive consent of the user is asked and the removed variables will be listed. We do not recommend passing this warning. Instead, if a stable variable is no longer needed, programers can promote its type to `Any` such that the upgrade works without warning. Please let me know if you encountered a case where this warning is not shown as this would be a bug.  
In the future, we plan more rigorous compatibility checks on upgrade that cannot be bypassed by a warning. However, I appreciate your suggestions as this would indeed be a useful improvement if `dfx` requires users to explicitly name the stable variables that should be deleted.

---

<div class="post-metadata">

**Author:** ![luc-blaeser](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/luc-blaeser/32/7410_2.png) [@luc-blaeser](https://forum.dfinity.org/u/luc-blaeser)\
**Post date:** [August 21, 2024, 3:01pm UTC](https://forum.dfinity.org/t/stable-variables-persistence/34039/3 "2024-08-21T15:01:04Z")

</div>

Hi @jaxopaxo,  
A follow up: I just noticed that the `dfx` warning is not displayed in case of dropped stable variable, although it is implemented in the corresponding internal Motoko check function. This seems to be indeed an issue. I’ll check with the team that we can fix this. I’ll keep you posted.

Thank you very much for reporting this!

---

<div class="post-metadata">

**Author:** ![luc-blaeser](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/luc-blaeser/32/7410_2.png) [@luc-blaeser](https://forum.dfinity.org/u/luc-blaeser)\
**Post date:** [August 23, 2024, 4:31pm UTC](https://forum.dfinity.org/t/stable-variables-persistence/34039/4 "2024-08-23T16:31:08Z")

</div>

I wonder which `dfx` and Motoko version you are using when observing the issue that `dfx` does not warn about dropping stable variables?
