[Ethereum] Delete all elements from an array


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[]) {
   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.

Best Answer

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

Related Topic