18

I am writing a smart contract to be the backend of a game. Instead of using a mapping, I have decided to use an array to store the addresses of accounts who have paid my contract. (I know it is common practice to use a mapping for this but I find them to be confusing)

address[] paidPlayers;

As players join, their address is added via the following function.

function addPlayer(address player) constant returns (address[]) {
   paidPlayers.push(player);
   return paidPlayers;
}

Periodically, when a round in the game is completed, I want my contract to reset. I thus need a way of emptying my paidPlayers array. How is this done in solidity? Is there anything like the javascript

paidPlayers = [];

I have found methods for deleting a specific element but not the array as a whole.

4 Answers 4

35

From Solidity Documentation Tips and Tricks

Use delete on arrays to delete all its elements.

delete paidPlayers;

This is exactly the same as paidPlayers.length = 0

8
  • 1
    Caveat! if the array is too large it will cause a run out of gas error! An alternative is to use mappings of uint256 and a totalNumberOfElements to handle where to save the values. In case of clear/reset you set totalNumberOfElements = 0. For inserts, you should use myArray[totalNumberOfElements] = value
    – imazzara
    Commented Jan 3, 2019 at 15:06
  • 2
    Right, but it depends on how you manage the access to them. I mean, you can let users only to get items with something like getItemAtIndex where you check the bounds with the valid totalNumberOfElements instead of making the mapping public. The rule should be: "If you are not confident about the size of the array, you will need to use mappings to prevent a possible DoS." Moreover, it saves gas, cause you are re-using the slots instead of empty all of them.
    – imazzara
    Commented Jan 3, 2019 at 16:02
  • 1
    After deleting an array; how much gas refund will be made? would it be 15.000 or the 15.000 * length_of_the_array ? Please see (ethereum.stackexchange.com/q/69882/4575) for more detail in question. @Ismael
    – alper
    Commented Apr 19, 2019 at 19:02
  • 1
    @LukeHutchison The question is about arrays not mappings. It is not possible to delete all elements from a mapping because keys aren't stored.
    – Ismael
    Commented May 31, 2022 at 3:14
  • 1
    @Ismael correct, I know that -- I was just offering some related information. Sorry if you thought it was off-topic. Commented Jun 1, 2022 at 4:19
6
You can manually set the array length to 0 with:
paidPlayers.length = 0;

This will clear all entries in the array.

Update: starting with Solidity v0.6, the length member is read-only. You can no longer delete an array by setting the length to zero.

3
  • 15
    Note that from Solidity 0.6 onwards the length member is read-only. Using delete is now the right approach.
    – hiddentao
    Commented May 14, 2020 at 12:58
  • 2
    This answer is no more relevant Commented Jul 12, 2020 at 13:14
  • 1
    @HaseebSaeed I have updated the answer now. Commented Oct 3, 2022 at 9:47
2

you can assign the array to a new empty array

   paidPlayers=new address[](0);
2
  • 1
    Is this more or less gas efficient than just deleting the array using Ismael's solution? Commented Oct 3, 2022 at 9:31
  • 1
    Update: it turns out assigning to an empty array is less gas efficient than using delete. To see this you can run this code snippet on Remix. The Foo contract's method cost 39,951 gas, while the Bar contract's method cost 40,030 gas. Commented Oct 3, 2022 at 9:36
0

You can manipulate the array using assembly assuming it's stored in memory...

function _resizeArray(
    address[] memory array,
    uint256 size
) internal pure {
    assembly {
        mstore(array, size)
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.