I have used in burn() function of NFT Contract.

 function burn(address to, uint256 tokenId) external {
    require(_exists(tokenId), "Contract: operator query for nonexistent token");
    for (uint256 i = 0; i < _tokenIds.length; i ++) {
        if (_tokenIds[i] == tokenId) {
            _tokenIds[i] = _tokenIds[_tokenIds.length - 1];
    // Clear metadata (if any)
    delete _Info[tokenId];
    delete _tokenOwnersMap[tokenId];
    delete _tokenApprovalsMap[tokenId];

    emit Burn(to, tokenId);

According to some resources on internet, loops and use of delete cause more gas consumption, is that correct? Any other alternative for delete?

  • You should pass to the function start index and end index. It will allow you to avoid the problem of transaction becoming larger than block size once the array is too long. Of course, the part where you actually delete the data should take place only when the last elements are removed. Oct 29 '20 at 10:54

Not entirely correct. In fact, delete reduces gas costs. Read about gas refunds for example here: https://ethereum.stackexchange.com/a/859/31933

The problematic part is looping. That has two main issues:

  1. If you loop based on length of an array (or any value which is not predetermined) the loop may run for too long if there are lots of entries and the transaction runs out of gas

  2. Structures which require a loop are often expensive to use due to the loop

Depending on the requirements it is sometimes possible to remove a loop (and its array) and use mappings instead. But that's only true if your data structure can be represented with a mapping and if you don't really need to iterate over it (you can't iterate over a mapping).

  • Thank you for correcting me about delete. But can you tell me for the above example? Is it efficient if I use mapping instead loop for that? Oct 29 '20 at 9:51
  • In your example, as it's a list of things, array is probably the only valid choice. Oct 29 '20 at 10:26
  • You did not suggest the most straightforward approach from problem #1 in your answer - passing to the function start index and end index (or start index and number of elements). Oct 29 '20 at 10:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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