Sodium Canister Upgrade deleted my state

I had some state in a canister deployed to Sodium, and I ran this command to update the frontend code while maintaining the state: dfx canister --network ic install --all --mode upgrade. But my state seems to have been wiped.

2 Likes

States are not preserved across upgrades by default. In Motoko, you can use the stable keywords to specify which variables to preserve: https://sdk.dfinity.org/docs/language-guide/actors-async.html#_stable_and_flexible_variables. In Rust, you need to provide pre_upgrade and post_upgrade functions, see https://github.com/dfinity/cdk-rs/blob/master/examples/asset_storage/src/asset_storage_rs/lib.rs as an example.

3 Likes

I see, I don’t think the documentation mentioned that. The documentation makes it seems like an upgrade just reserves state automatically.

1 Like

Thanks for catching this,@lastmjs. This is an oversight and should definitely be mentioned in the documentation for upgrading a canister!

Please feel free to log issues like this if the docs repo too.

3 Likes