1

I understand that when you use the delete() function on a struct it will delete all its data excluding its structs, as delete cannot recurse through these. Does this mean we can still access these mappings within the struct or will their data simply float around in the state?

  • 1
    The data will be set to 0 (i.e., each field will be set to "the zero" value of its type). – goodvibration Mar 18 '19 at 10:41
2

Delete does not clear mappings within struct because there is no way to iterate over mapping entries. BTW, delete is not a function, and it does recurse into struct members that are structs themselves. It does not recurse into dynamic arrays, but trims such arrays to zero length. So yes, after deleting struct, you may still access mappings inside it and all the data will be there. Here is an example:

struct A {
  mapping (uint => uint) map;
}
A private a;
function foo () public {
  a.map[5]=7;
  delete a;
  assert (a.map[5]==7); // Will not throw!
}
| improve this answer | |
  • Might be worth adding a coding example which deletes a struct and ten accesses a mapping inside that struct. – goodvibration Mar 18 '19 at 11:17
  • Very nice! ..... – goodvibration Mar 18 '19 at 12:36
  • Thanks for this, I will try it out in different cases but it looks straight forward. – Oliver Chalk Mar 18 '19 at 12:40

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.