3

In my pallet, I have a StorageDoubleMap called MyDoubleMap. I want to delete all the kv pairs under key1. So I may write the code like below:

for (key2, _value) in MyDoubleMap::<T>::iter_prefix(key1) {
    MyDoubleMap::<T>::remove(key1, key2);
}

Apparently the map was changed during the iteration. Will it be problematic?

1 Answer 1

4

As the docs are mentioning, you will get undefined results. If you want to remove all these elements, you should directly use drain_prefix as this will give you directly what you want.

2
  • Is the same true for mutating the StorageItem? I.e. set, or insert? Commented Aug 11, 2022 at 16:02
  • The docs explicitly mention "adding". So, yes insert is also problematic. However, setting should be fine.
    – bkchr
    Commented Aug 12, 2022 at 9:43

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.