I have a mapping with the following structure:

mapping(uint256 => mapping(address => SOME_STRUCT)) public submissions;

delete submissions[uint256] is not allowed(compiler error).

delete submissions[uint256][msg.sender] is allowed(no compiler error).

The remove function needs to be restricted i.e only the manager of the contract is allowed to delete from the submissions mapping. But the address in the inner mapping serves as the key and is an arbitrary address. How would i go about deleting the inner mapping?


From the official documentation:

delete a assigns the initial value for the type to a, i.e. for integers it is equivalent to a = 0, but it can also be used on arrays, where it assigns a dynamic array of length zero or a static array of the same length with all elements set to their initial value. delete a[x] deletes the item at index x of the array and leaves all other elements and the length of the array untouched. This especially means that it leaves a gap in the array. If you plan to remove items, a mapping is probably a better choice.

For structs, it assigns a struct with all members reset. In other words, the value of a after delete a is the same as if a would be declared without assignment, with the following caveat:

delete has no effect on mappings (as the keys of mappings may be arbitrary and are generally unknown). So if you delete a struct, it will reset all members that are not mappings and also recurse into the members unless they are mappings. However, individual keys and what they map to can be deleted: If a is a mapping, then delete a[x] will delete the value stored at x.

To answer your question How would i go about deleting the inner mapping?:

The entity authorized to delete the mapping should either store the mapping keys, or receive them as input when it is requested to initiate the process.

| improve this answer | |
  • The removal process is never initiated by the owner of the address used as the key in the inner mapping. Basically the scenario is that i'd like to remove everything associated with the uint256 used as the key in the outer mapping. I suppose i could just leave it in there though, since the key can never be used again but leaving stuff in there as garbage is bad practice? – Conviley Apr 13 at 12:35
  • @Conviley: You could earn gas by deleting those (though you'll actually earn gas only if you do it from a transaction which spends at least twice more). – goodvibration Apr 13 at 12:38
  • Yeah that's what i though about - the gas refund. But i think you're right in the sense that it will cost more to delete than the refund itself. Because then i'd have to save all the addresses and link them to the outer uint256 key and then like iterate through all of them to remove their entry in the inner mapping. I'll just leave it as garbage value then i guess :/ – Conviley Apr 13 at 12:43
  • What I meant to say is that you don't get refund if it's more than (even half of) the cost of the rest of the transaction, i.e., a transaction will obviously not end up with the miner paying you instead of vice-versa. And of course, I haven't even taken the cost of storing the keys on-chain for specifically for that purpose (I've assumed that you would save them on your off-chain server and pass them to the manager when you want to delete them from the mapping). – goodvibration Apr 13 at 12:50
  • yeah saving them off chain could be something but it's a bit outside the scope of my little project, I believe I'll just leave the values there it won't cause a problem since the keys cannot be reused. Nevertheless, I'll go ahead and accept your answer because I feel it is indeed correct! :) – Conviley Apr 13 at 13:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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