There is something is wrong with my delete function or i do not know what i have missed.

The id of the user is stored in both User (struct) and userIds (array) and i want to be able to delete the id from userIds and the entire user(id, userInfo, userData;) from my block chain. my current function does not really delete the user :(

function delete_user_byId(uint _id) public returns(uint[]) {
    if (_id >= userIds.length) return;
    for (uint i = _id; i<userIds.length-1; i++){
        userIds[i] = userIds[i+1];
    delete users[_id];
    delete userIds[userIds.length-1];
    return userIds;


2 Answers 2


I'm not sure why doesn't seem to work. There could be other factors in play such as testing methodology. Are you waiting for the transaction to mine before inquiring about the new state?

In any case, the pattern won't scale because of the unbonded for loop. There's also an upper bound on the size of the uint[] to return, so returning more than one record at a time should be used with caution.

Your mapping/struct approach can support a delete if you needed it. Have a look over here: https://medium.com/@robhitchens/solidity-crud-part-2-ed8d8b4f74ec

There are simpler ways to accomplish similar things, so consider how important delete is in the context of the app (nothing is ever really deleted) check out the pros and cons of different approaches: Are there well-solved and simple storage patterns for Solidity?


I want to add one more important point to your notice that delete is actually not really deletion at all - it's only assigning a default value back to variables. It will assign default values of attributes of user namely id, userInfo and userData depending on the respective data type.

Refer the following documentation for more details -


Understanding more about delete keyword in solidity

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.