i am trying to verify in a contract method, that a struct exists in a mapping while having it's address. here is what i tried. Note : i have the UserAddress.

function DeleteAUser(address UserAddress) onlyOwner{
    require(users[UserAddress] > 0);
    delete users[UserAddress] ;


function DeleteAUser(address UserAddress) onlyOwner{
    require(users[UserAddress] != bytes4(0x0));
    delete users[UserAddress] ;

EDIT : As you can see i only have the Address as input to the function.

but still has an error of operator compatibility :

TypeError: Operator == not compatible with types struct Ownership.User storage ref and bytes4

Is there anyway to solve this ?

  • Can you provide more code? What is your struct? Can't you just check if one of the field exits? Like require(users[address].exists == true) Feb 15, 2018 at 9:13
  • well your solution is quite a workaround actually, and using workarounds it not always the best idea and it needs me to add an other property exist to my struct. Feb 15, 2018 at 9:27
  • But the thing is that if the struct you are trying to delete does not exists, it wont delete anything. It will not cause any problem Feb 15, 2018 at 9:28

1 Answer 1


There's no way to test for the existence of something in a mapping. In the EVM, anything that hasn't already been written to in storage has the value 0.

So you'll always get back a struct when you read from the mapping; all the values will just be zero.

If you can just test for a zero (e.g. users[UserAddress].balance == 0 is sufficient), go ahead and do that. If you need something more definitive, use an explicit existence bool like !users[UserAddress.balance].exists.

  • How about if there is multiple mappings, such as users[userAddress][job][index] ? @smarx
    – alper
    Sep 6, 2018 at 17:59
  • That doesn't change the answer.
    – user19510
    Sep 6, 2018 at 18:06
  • If storage reference does not exist this seems like does not work.
    – alper
    Sep 6, 2018 at 18:10
  • You may need to ask a new question so you can share your code and explain further. I don't know what you mean by "does not exist."
    – user19510
    Sep 6, 2018 at 18:19
  • 1
    In that case, you had an array somewhere, not a mapping.
    – user19510
    Sep 6, 2018 at 18:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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