How can I delete the data in TiKV directly?

691 Views Asked by At

I used tikvTxn to write the key-value data into TiKV directly and skip the TiDB.

db, err := driver.Open("tikv://127.0.0.1:2379?disableGC=true")
txn, _:= db.Begin()
txn.set(key, value)
txn.commit(context.Background())
...

I can't clean the data in TiKV by drop the tables in TiDB.

How can I delete all the data that I inserted to TiKV?

1

There are 1 best solutions below

0
On

To delete the data inserted via txnkv API, you can:

db, _ := driver.Open("tikv://127.0.0.1:2379?disableGC=false")
txn, _ := db.Begin()
txn.Delete(key)
txn.Commit(context.Background())
...

Txnkv is based on MVCC, so Delete will not reclaim disk space. Instead, it inserts a special version which indicates the key has been deleted.

If there is a TiDB in your cluster, and it enables GC, then the key will be deleted physically and automatically after a GC interval.

Otherwise you need to run GC job to delete it from disk.

import "github.com/pingcap/tidb/store/tikv/gcworker"

gcworker.RunGCJob(ctx context.Context, s tikv.Storage, safePoint uint64, identifier string, concurrency int)