Skip to main content

how-to-delete-data-from-a-multi-index-table

Overview

This guide provides instructions to to delete data from a multi-index table.

Reference

See the following code reference:

Before you begin

Make sure you have the following prerequisites in place:

  • An Antelope development environment, for details consult the Get Started Guide,
  • A multi-index testab table instance which stores user objects indexed by the primary key which is of type eosio::name. Consult the section How to instantiate a multi-index table to learn how to set it up.

Procedure

Complete the following steps to implement a del action which deletes an user object, identified by its account name, from the multi-index table.

1. Find The User You Want To Delete

Use the multi-index find(...) method to locate the user object you want to delete. The targeted user is searched based on its account name.

[[eosio::action]] void multi_index_example::del( name user ) {
// check if the user already exists
auto itr = testtab.find(user.value);
}

2. Delete The User If Found

Check to see if the user exists and use erase(...) method to delete the row from table. Otherwise print an informational message and return.

[[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 );
}
Full example location

A full example project demonstrating the instantiation and usage of multi-index table can be found here.

Summary

In conclusion, the above instructions show how to delete data from a multi-index table.

Next Steps

  • You can verify if the user object was deleted from the multi-index table. .
  // check if the user was deleted
auto itr = testtab.find(user.value);
if ( itr == testtab.end() ) {
printf("User was deleted successfully.");
}
else {
printf("User was NOT deleted!");
}