Delete Data from a Multi-Index Table
Overview
This guide provides instructions for deleting data from a multi-index table.
Prerequisites
- Before proceeding, ensure that you have completed the Getting Started section and that you have followed the Getting Started Documentation Diagram.
- This page assumes you are familiar with Smart Contract Basics.
- A multi-index
testtab
table instance which storesuser
objects indexed by the primary key which is of typesysio::name
. Consult the section How to instantiate a multi-index table to learn how to set it up.
Steps
Complete the following steps to implement a del
action which deletes a 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.
[[sysio::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.
[[sysio::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);
}
A full example project demonstrating the instantiation and usage of multi-index tables can be found in the multi_index example
project.
Reference
See the following code reference:
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!");
}