If I have a struct with many customer accounts:

bytes32[] public customerID;
mapping(bytes32 => CustomerAccounts) public customerAccounts;

struct CustomerAccounts{
   address customer;
   uint balance;

To delete a customer account in the struct, is it better to assign to a set of default values, like this:

CustomerAccounts memory blank;
customerAccounts[customerID] = blank;

or to use delete, as in this:

delete customerAccounts[customerID];

Or does not not really matter?

While the difference in gas is small, < 1000, I suspect in the former case, it may be still there in some ghostly form that could cause problems.


Better the second option, it's cleaner and more efficient. It sets all bytes of the struct to zero, and makes the code easier to maintain.

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.