longtech
longtech

Reputation: 93

How to clear and reset Mapping of Array in solidity

Below is how I do the record into the mapping array. How can I create a clear|removeall function that clears or reset all the records back to default which is empty?

    address payable[] public players;
    mapping(address => uint256[])  playerTicket;
        
        
    function playersRecord() public view returns(uint256[] memory){
                return playerTicket[msg.sender];
    }

I managed with the below function to clear one by one but not sure how to clear all

function remove(address _addr) public {
    // Reset the value to the default value.
    delete playerTicket[_addr];
}

Upvotes: 3

Views: 4729

Answers (2)

vikash ruhil
vikash ruhil

Reputation: 11

use delete keyword in latest version. working example - refer this repo

delete players;

Upvotes: 0

Antonio Carito
Antonio Carito

Reputation: 1387

You cannot clear all mapping values without specificy the key. Thus, Solidity doesn't know the keys of the mapping. Since the keys are arbitrary and not stored along, the only way to delete values is to know the key for each stored value.
Your remove() function is correct to clear the values of a specific mapping key.

More information here.

Upvotes: 3

Related Questions