Is it safe to delete a dynamic array in a mapping?

Hey guys, here is a quick question on if it is safe to delete the entire mapped dynamic array and then create a new one for the same key. See the following code 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 it would be great if anyone could provide 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);        
    }
}
1 Like

The delete in this code is fine.

1 Like