After deleting a key using the txn method in the TIKV client.go client, can it still be found in the scan prefix?

This topic has been translated from a Chinese forum by GPT and might contain errors.

Original topic: 使用TIKV client.go客户端 txn方法删除key后,在scan prefix还能查到?

| username: 夜-NULL

The client.go client of TIKV calls the Delete method to delete a key (“$uat-zmc-xxxxxx”)

func (txn *KVTxn) Delete(k []byte) error {

Then, using the scan method to query data with the prefix “$”, the just-deleted key can still be found.

func scanKeys(prefix []byte, tx *tikv.KVTxn) [][]byte {
	it, err := tx.Iter(prefix, nextKey(prefix))
	if err != nil {
	defer it.Close()
	var ret [][]byte
	for it.Valid() {
		ret = append(ret, it.Key())
		if err = it.Next(); err != nil {
	return ret

The transaction has already been committed during the delete:

if !tx.IsReadOnly() {
		err = tx.Commit(context.Background())

May I ask why this is happening? Is it only deleting the latest version in MVCC?
How can I completely delete this key?

| username: xfworld | Original post link

Delete is just adding a record; complete deletion has to wait until after compaction.

You might ask, when does compaction get triggered? Check out this document:

If you are using TiKV version 5.4 or later, manual compaction is supported. If it’s a test environment, you can give it a try:

| username: 夜-NULL | Original post link

Thank you~

I have a question. Theoretically, appending data shouldn’t affect scan and get queries, right? Not doing compaction just means not freeing up memory, which is understandable, but there should definitely be some kind of deletion marking, right?

| username: xfworld | Original post link

It seems that TiDB 4.0.x has fixed the issue of skipping data marked as Delete during Seek. I forgot which specific version fixed it.

| username: 夜-NULL | Original post link

Currently using TiKV version 5.0.6~~
The client-go version is v2.0.1~~
Still a bit confused as to why it can still scan data that has already been (txn) deleted~

| username: xfworld | Original post link

Well, that means the standard TiDB handling method has been fixed, but it does not include RawAPI and TxnAPI :rofl:

Looks like we need to handle it ourselves…

| username: 夜-NULL | Original post link

Okay, thx~~

| username: system | Original post link

This topic was automatically closed 1 minute after the last reply. No new replies are allowed.