0

I`m looking for an efficient way to purge StorageDoubleMap.

so lets assume I have some collection:

#[pallet::storage]
#[pallet::getter(fn provisions)]
pub type Provisions<T: Config> =
    StorageDoubleMap<_, Twox64Concat, T::AccountId, Twox64Concat, TokenId, Balance, ValueQuery>;

That is filled with some data and now I want to remove all the entries effectively.

What I tried so far is:


        #[pallet::weight(T::WeightInfo::claim_rewards())]
        #[transactional]
        pub fn finalize(origin: OriginFor<T>, limit: Option<u32>) -> DispatchResult {
            ensure_root(origin)?;

            let mut remove_counter = 0;

            for prefix in [
                Provisions::<T>::prefix_hash(),
            ] {
                match child::kill_storage(&ChildInfo::new_default_from_vec(prefix), limit) {
                    KillStorageResult::AllRemoved(num_removed) | 
                    KillStorageResult::SomeRemaining(num_removed) => {
                        if let Some(l) = limit {
                            remove_counter+=num_removed;
                            if  remove_counter >= l {
                                Self::deposit_event(Event::PartiallyCleaned);
                                return Ok(().into())
                            }
                        }
                    }
                };
            }
            Self::deposit_event(Event::Cleaned);
             Ok(().into())

        }

but it doesn't seem to work.

1
  • why are you calling child::kill_storage?
    – Shawn Tabrizi
    Commented Jun 24, 2022 at 2:59

1 Answer 1

2

StorageDoubleMap has a function:

/// Remove all value of the storage.
pub fn remove_all(limit: Option<u32>) -> sp_io::KillStorageResult {
    <Self as crate::storage::StoragePrefixedMap<Value>>::remove_all(limit)
}

https://crates.parity.io/frame_support/pallet_prelude/struct.StorageDoubleMap.html#method.remove_all

So you can just call let kill_result = Provisions::<T>::remove_all(limit);

Then, check if kill_result is AllRemoved.

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.