What's the best way to clear a mapping? Consider the following case:

struct user {
    string name;
    uint id;
    bool valid;
    bytes32 hash_id;

mapping (address => user) userRegister;

function clearStruct (address addr) constant returns (bool cleared) {  
   userRegister[addr].name = null;  
   userRegister[addr].id = null;  
   userRegister[addr].valid = null;  
   userRegister[addr].hash_id = null; 

I am looking for a generic way to clear structs. the error i see right now at = null:

Error: Expected primary expression

Anyone who could suggest, it would be greatly helpful. is the only way to make them corresponding nulls i.e. '' for string, 0x000... for bytes32, false for bool, and 0 for int and so on?


you could use delete to clear your map.

delete userRegister[addr];

proposition :

pragma solidity ^0.4.0;
contract test {

struct user {
    string name;
    address add;

mapping (address => user) public userRegister;

 function add_user (string name, address addr)   {  

function clearStruct (address addr)   {  
   delete userRegister[addr];  


function try_it (address addr)  returns (string name) {  

   return userRegister[addr].name ;
| improve this answer | |

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.