0

I have a bunch of mapping{address => uint256) in my contract. I expect that during normal use, these mappings will be added and removed frequently. When a mapping is no longer needed, is it necessary that I explicitly delete it? Or is it ok if I just leave it there? My understanding is that once a mapping is stored, leaving it there will not cause any problems or consume any gas unless it is accessed. Is this correct?

In any other language I would worry about the collection getting big and cluttering things up. But, since everything stored in the whole history is permanent, "deleting" a mapping is actually performing another write to set the value to 0, and will take more space rather than less.

Note: I am aware that this question sounds almost identical to this one, however they are slightly different. I am asking about storage costs, they are asking about how the delete keyword works. The other question also has an accepted "answer" that doesn't actually answer mine at all - they tested in a debugger, and found that their debugger does not retain data once there are no pointers to it in the current scope. As I am not using their debugger, their results might not apply to me. I am specifically asking in the context of the ethereum blockchain, which I'm 99% sure does not have an automatic garbage collector built in... Does it?

Benubird
  • 193
  • 6

1 Answers1

1

Leaving it is fine. (As you say, there's no ongoing gas cost.)

Deleting it (setting it to zero) gives you a gas refund, so there's incentive to do that.

user19510
  • 27,999
  • 2
  • 30
  • 48