BEFORE YOU DOWNVOTE
I know questions on deleting mappings have come up thousands of times but I'd like to pose a question... can you delete it if it's in an array?
the following code allows the caller to delete a nested list of addresses
pragma solidity ^0.8.0;
contract Arrs {
address[][] private _addy;
constructor() {
_addy.push();
_addy[0].push();
}
function def() public {
_addy[0][0] = msg.sender;
}
function del() public {
delete _addy; // or _addy.pop()
_addy.push();
_addy[0].push();
}
function value() public view returns (address) {
return (_addy[0][0]);
}
}
running value()
initially gives you the zero address, def()
sets it to your address, del()
resets it. It works great.
Now I KNOW that you cannot delete entire mapping arrays, however, I am not getting any errors or warnings in Remix/other IDEs and no errors or reverts when running the functions on a deployed contract on Goerli when I try and delete an array that contains the mapping.
contract Maps {
mapping(uint256 => address)[] private _map;
constructor() {
_map.push();
}
function def() public {
_map[0][0] = msg.sender;
}
function del() public {
delete _map;
_map.push();
}
function value() public view returns (address) {
return (_map[0][0]);
}
}
running value()
initially gives you the zero address, def()
sets it to your address, but del()
is where it gets interesting.
In debugging on Remix, it seems that delete _map
actually sets the _map
array length to 0. However, when _map.push()
is called, it somehow recovers the old mapping so when value()
is called, is returns your address, not the 0 address.
I was wondering why this was and if there was a way to get around it or if I'll have to keep with the status quo and iterate through the mapping via a second array keeping track of the keys.