0
    mapping(uint => address) public Players;

can anybody help me how can I delete Players mapping. I wrote a lottery contract and I need to remove this mapping to reuse the contract.

I try this code but It doesn't work

    delete Players[playerId];

1 Answer 1

2

Solidity is not like the other languages. You can think of the storage as empty array so even if you have never used a mapping for eg. Players[12334] its value is not nil/null/nullptr it is the default value for the type. In your case the default value for address is the zero address (0x000.../address(0)). If you want to delete player it is enough to set it to the zero address.

2
  • yeah that works but still there is a problem . Let say my contract has 1 million player , In this case I should use for Loop and equal every address to zero. Is there a way to remove my mapping without using for loop ???
    – user83980
    Oct 23, 2021 at 5:22
  • Unfortunately there is no other way. And deleting all users is a corner case, most probably you will delete only one at a time and you will know which is its index. Oct 23, 2021 at 5:46

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.