Hey guys, here is a quick question on if it is safe to delete the entire mapped array in a mapping and create a new one. See the following snippet for an example.
I once read somewhere on dynamic arrays in a mapping that although the pointer is deleted, the values are not, therefore, the same mapping may return unexpected values. In this simple test, it worked fine, but would like to get in-depth explanations. Thanks.
// SPDX-License-Identifier: MIT
pragma solidity 0.8.9;
contract Test {
mapping(uint256 => uint256[]) public _ideaPool;
function add(uint256 epochId, uint256[] memory ideaIds) public {
uint256[] storage ideas = _ideaPool[epochId];
require(ideas.length == 0, "Invalid epochId");
uint256 n = ideaIds.length;
for (uint256 i = 0; i < n; i++) {
ideas.push(ideaIds[i]);
}
}
function update(uint256 epochId, uint256[] memory ideaIds) external {
delete _ideaPool[epochId];
add(epochId, ideaIds);
}
}