'Can't reset Solidity contract array variable

I'd like to reset the players dynamic array variable in the following contract:

pragma solidity ^0.4.17;

contract Lottery {
    address[] public players;
 
    function enter() public payable {
        require(msg.value > .01 ether);
        players.push(msg.sender);
    }
    
    function pickWinner() public {
        players[0].transfer(this.balance);
        players = new address[](0); // <------- HERE
    }
    
}

Pushing new addresses in the array works just fine, but resetting it to an empty array doesn't work: I get a The transaction ran out of gas. Please increase the Gas Limit. error (even when I increase the gas limit). I've also tried delete players and players.length = 0 but nothing works.

Any suggestion?



Solution 1:[1]

Your code just works fine for a 5-element array but the longer array you get, the more gas transaction consumes.

In this case, you can increase your gas limit. There is no problem with your code.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Yusuf Sina Y?ld?z