eos icon indicating copy to clipboard operation
eos copied to clipboard

How to delete table data from the contract account

Open yhc166188 opened this issue 8 years ago • 5 comments

After I clear out the contract, the table data under this contract still exists, and also occupies RAM, I do not know with what command delete table data

yhc166188 avatar Oct 16 '18 10:10 yhc166188

void deletedata(){

require_auth(_self);
<table_name> st(_self, _self);
auto itr = st.begin();
while(itr != st.end()){
    itr = st.erase(itr);
}

} after this cleos push action <contract_name> deletedata [ ] -p <contract_name>

kstvigilante avatar Oct 16 '18 12:10 kstvigilante

There is no generic command to remove contract data. As kstvigilante describes, you must create a contract with a delete action, then invoke the action.

If your table is so large that deleting it all at once would exceed the CPU usage limitations of a transaction, you'll need to add a counter to the above code and remove rows in batches. This may also be necessary if you don't have many tokens staked for CPU usage.

jgiszczak avatar Oct 16 '18 17:10 jgiszczak

There is no generic command to remove contract data. As kstvigilante describes, you must create a contract with a delete action, then invoke the action.

If your table is so large that deleting it all at once would exceed the CPU usage limitations of a transaction, you'll need to add a counter to the above code and remove rows in batches. This may also be necessary if you don't have many tokens staked for CPU usage.

Can we write a general command to delete a table?

yhc166188 avatar Oct 17 '18 01:10 yhc166188

void deletedata(){

require_auth(_self);
<table_name> st(_self, _self);
auto itr = st.begin();
while(itr != st.end()){
    itr = st.erase(itr);
}

} after this cleos push action <contract_name> deletedata [ ] -p <contract_name>

What should I do if I want to delete the table data of other contracts in the system contract?

yhc166188 avatar Oct 17 '18 08:10 yhc166188

Generally speaking, you shouldn't be trying to. On public nets, you have no option to customize the system contract.

On a private network, the only difference to kstvigilante's code would be in the declaration of the table, which would substitute an arbitrary account name for the first _self argument. This could conceivably be an argument to the contract action.

jgiszczak avatar Oct 17 '18 18:10 jgiszczak