---
# System prepended metadata

title: Clear Prefix version 2

---

# Clear Prefix version 2

Clear the storage of each key-value pair where the key starts with the given prefix.
## Limit
Deletes all keys from the overlay and up to limit keys from the backend if it is set to Some. No limit is applied when limit is set to None.
The limit can be used to partially delete a prefix storage in case it is too large to delete in one go (block).
It returns a boolean false iff some keys are remaining in the prefix after the functions returns. Also returns a u32 with the number of keys removed from the process.
### Note
Please note that keys that are residing in the overlay for that prefix when issuing this call are all deleted without counting towards the limit. Only keys written during the current block are part of the overlay. Deleting with a limit mostly makes sense with an empty overlay for that prefix.
Calling this function multiple times per block for the same prefix does not make much sense because it is not cumulative when called inside the same block. Use this function to distribute the deletion of a single child trie across multiple blocks.


This, clear_prefix  version2 will return 2 values, 
1. Boolean (true if all keys with prefix got deleted else false)
2. Number of keys deleted from DB

https://github.com/paritytech/substrate/blob/master/primitives/io/src/lib.rs#L145

If the limit is none, we are deleting all, The code logic is written inside limit_remove_from_backend. Basically in substrate, it is iterating all keys with the given prefix and deleting each from the db by passing NONE value to set_child_storage and set_storage
https://github.com/paritytech/substrate/blob/master/primitives/state-machine/src/ext.rs#L818

#### Links for functions used in substrate code
[clear_prefix()](https://github.com/paritytech/substrate/blob/master/primitives/state-machine/src/ext.rs#L479)
[limit_remove_from_backend()](https://github.com/paritytech/substrate/blob/master/primitives/state-machine/src/ext.rs#L788)
[apply_to_keys_while()](https://github.com/paritytech/substrate/blob/master/primitives/state-machine/src/trie_backend_essence.rs#L273) 
[trie_iter_inner()](https://github.com/paritytech/substrate/blob/master/primitives/state-machine/src/trie_backend_essence.rs#L273)

https://github.com/paritytech/substrate/blob/master/primitives/state-machine/src/ext.rs#L480


Not sure for this, But this call deleting all occurrence of keys with the given prefix from overlay (might be runtime trie)
[self.overlay.clear_prefix(prefix);](https://github.com/paritytech/substrate/blob/master/primitives/state-machine/src/ext.rs#L479)


This is for the trie stored in backend, This will take time so new api has been introduced to overcome the slowness of deletion from backend.

[self.limit_remove_from_backend(None, Some(prefix), limit)](https://github.com/paritytech/substrate/blob/master/primitives/state-machine/src/ext.rs#L480)

The code is iterating the keys for the prefix and for each key, it is setting none value using set_child_storage or set_storage.


Introduce new api ext_storage_clear_prefix_version_2, which accept prefix and limit as parameters. This api will first delete all keys with prefix from trie, and then it will call limit_remove_from_backend which iterate the keys from db with the prefic and remove each key till limit reached.

