The IterableStorageMap API explicitly forbids altering the map while iterating: https://paritytech.github.io/substrate/master/src/frame_support/storage/mod.rs.html#270-272

The frame_support::storage::migration module is less clear in its documentation. If I iterate over a storage map using storage_iter, what's the effect of altering the map while iterating over it? What changes to the map can I make without causing undefined behavior?

1 Answer 1


Iterating over a storage map is going through all the storage keys in order based on their hex representation.

If you are iterating over a storage map, and add a new item to the storage map, it is possible you will or will not go over that item during iteration, which as you stated is an undefined behavior.

There is not really a way around this since most storage map keys use a hash, which have no predictable output.

The only thing you can safely do is modify existing values of the map in place, without modifying the keys present, which will keep the iterator consistent.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.