Preconditions
- It is assumed you already have a multi index table instance defined along with its mandatory primary index, otherwise take a look at the section How to instantiate a multi index table.
To delete data from a multi index table follow the steps below:
- Make use of the multi index table iterator to find out if the data exists
[[eosio::action]] void multi_index_example::del( name user ) {
// check if the user already exists
auto itr = testtab.find(user.value);
}
- If the data exists use the
delete
method to delete the row from table
[[eosio::action]] void multi_index_example::del( name user ) {
// check if the user already exists
auto itr = testtab.find(user.value);
+ if ( itr == testtab.end() ) {
+ printf("user does not exist in table, nothing to delete" );
+ return;
+ }
+ testtab.erase( itr );
}