Solidityで配列の要素を削除する方法について解説します。
構文
Solidityで配列の要素を削除するには、popメソッドを使用します。
なお、削除されるのは、配列の最後の要素となります。
配列.pop();
サンプルコード
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract Test {
uint[] public array = [1, 2, 3];
function removeLastElement() public {
// 配列の要素を削除
array.pop();
}
function getArray() public view returns(uint[] memory) {
return array;
}
}

配列の追加方法(pushメソッド)と併せておさえておきましょう。