Ethereum Stack Exchange is a question and answer site for users of Ethereum, the crypto value and blockchain-based consensus network. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Anyone know how to delete an element in a array? Is there any built in method to do that?

If not, does anyone know of how to implement such a method?

share|improve this question
Do you want to just delete the element, or delete it and move everything down an index? – Tjaden Hess Feb 21 at 1:20
I would like everything down an index so if i delete a[2] then a[3] becomes a[2] – user697 Feb 21 at 1:42
up vote 11 down vote accepted

Use the delete operator to delete the element:

delete array[index];

If you don't want to leave a gap, you need to move each element manually:

contract test{
    uint[] array = [1,2,3,4,5];
    function remove(uint index)  returns(uint[]) {
        if (index >= array.length) return;

        for (uint i = index; i<array.length-1; i++){
            array[i] = array[i+1];
        delete array[array.length-1];
        return array;

If you don't care about the order, you can also just copy the last element into the empty spot, then delete the last element.

share|improve this answer
Thanks! I just have an issue with the function you gave when trying to make it more " general : function remove(uint[] array,uint index) returns(uint[]) { gives me Error: Expression has to be an lvalue. array.length--; Also , does that method can be adapted to work on array of all types ( struct , etc ) ? – user697 Feb 21 at 2:35
I'm not sure about decreasing the length of arrays in memory, as opposed to storage. You can copy it all over to a new array, I guess, or just remove that line and leave the trailing zeros. You won't save any gas by making a memory array shorter, anyway. This method should work on any type, except mappings, since delete doesn't really make sense in mappings. – Tjaden Hess Feb 21 at 2:41

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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