stable-memory

Persist Internet Computer canister state across upgrades using stable memory structures.

Updated Apr 3, 2026
One-click install
npx skills add https://github.com/phukrit7171/Relationship-Smart-Contract-ICP --skill stable-memory-phukrit7171
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: stable-memory
Source: https://github.com/phukrit7171/Relationship-Smart-Contract-ICP/tree/main/.agents/skills/stable-memory
Command: npx skills add https://github.com/phukrit7171/Relationship-Smart-Contract-ICP --skill stable-memory-phukrit7171

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Canister upgrades on the Internet Computer wipe heap memory, silently destroying user data stored in ordinary variables. This Skill shows how to store data in stable memory so it survives every redeploy, in both Rust and Motoko. ## Core Features & Use Cases - Rust stable structures: Implement StableBTreeMap, StableCell, and StableLog with MemoryManager partitioning, Storable trait implementations, and CBOR serialization via ciborium. - Motoko persistent actors: Use mo:core 2.0 persistent actor syntax where all let and var declarations auto-persist without stable keywords or upgrade hooks. - Upgrade trap prevention: Avoid pre_upgrade instruction-limit traps, MemoryId collisions, and schema-change failures that brick canisters. - Use Case: You deploy a Rust canister, add user records, redeploy with new code, and verify with get_user_count that all data survived the upgrade. ## Quick Start Ask the AI to implement stable memory persistence for your ICP canister so user data survives upgrades, then deploy and verify the record count is unchanged after redeploying.

Frequently Asked Questions about stable-memory

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I persist canister data across upgrades on the Internet Computer?

Store data in stable memory rather than heap memory. In Rust use StableBTreeMap from ic-stable-structures with a MemoryManager; in Motoko use a persistent actor from mo:core 2.0 where all let and var declarations persist automatically.

How do I use StableBTreeMap in a Rust canister?

Initialize a MemoryManager, assign each structure a unique MemoryId, and wrap the StableBTreeMap in thread_local RefCell. Keys need Storable plus Ord; values need Storable, typically implemented with ciborium CBOR serialization and Bound::Unbounded.

Why does my canister lose data after icp deploy?

Data stored in thread_local RefCell heap variables is wiped on every upgrade. Only data written to stable memory via stable structures or Motoko persistent actor declarations survives a redeploy.

What causes pre_upgrade to trap and brick a canister?

Serializing large heap data structures to stable memory inside pre_upgrade exceeds the fixed instruction limit for upgrade hooks, trapping and bricking the canister. StableBTreeMap writes directly to stable memory and needs no serialization step.

Can I reuse a MemoryId for two stable structures?

No. Each stable structure must have a unique MemoryId from the MemoryManager. Reusing an id makes two structures interpret the same stable memory bytes, silently corrupting each other's data.

Do Motoko persistent actors need the stable keyword?

No. In mo:core 2.0 persistent actors, all let and var declarations are automatically stable. Writing stable let produces warning M0218, and no pre_upgrade or post_upgrade hooks are required.