Skip to main content

Delete Data from a Multi-Index Table

Overview

This guide provides instructions for deleting data from a multi-index table.

Prerequisites

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.

contract.cpp
[[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.

contract.cpp
[[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);
}
info

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:

contract.cpp
// 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!");
}