# DB returning deleted keys value pair

**URL:** <https://discuss.dgraph.io/t/db-returning-deleted-keys-value-pair/15013>\
**Category:** Badger\
**Tags:** kind:question, kind:bug\
**Created:** [July 20, 2021, 4:56am UTC](https://discuss.dgraph.io/t/db-returning-deleted-keys-value-pair/15013 "2021-07-20T04:56:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![debasish-mihup](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/debasish-mihup/32/8606_2.png) [@debasish-mihup](https://discuss.dgraph.io/u/debasish-mihup)\
**Post date:** [July 20, 2021, 4:56am UTC](https://discuss.dgraph.io/t/db-returning-deleted-keys-value-pair/15013/1 "2021-07-20T04:56:55Z")

</div>

### What version of Go are you using (`go version`)?

go1.16 linux/amd64

### What operating system are you using?

Linux (Ubuntu 20.04)

### What version of Badger are you using?

[v3.2103.1](https://github.com/dgraph-io/badger/releases/tag/v3.2103.1)

### Does this issue reproduce with the latest master?

Yes

### Steps to Reproduce the issue

### What Badger options were set?

```auto
    badgerDBParentDirectory := "./BadgerPersistentKeyValueDB/"
	if _, err := os.Stat(badgerDBParentDirectory); os.IsNotExist(err) {
		_ = os.Mkdir(badgerDBParentDirectory, os.ModePerm)
	}

	options := badger.DefaultOptions(badgerDBParentDirectory)
	options.Logger = nil // Comment this out to print badger DB analytics
	options.NumVersionsToKeep = 1
	var err error
	badgerDB, err = badger.Open(options)
	if err != nil {
		log.Fatal(err)
	}

```

### What did you do?

I use below three functions to store a single key value pair, delete a single key (and corresponding value) and return “n” number of key value pair from the DB. The GetNKeyValues function still seem to be returning deleted keys value pair in spite of all checks in place. This seem to happen only during the stress testing, does not happen when testing with few key value pair or low system load.

```auto
func StoreKeyValue(key, value []byte) error {
	err := badgerDB.Update(func(txn *badger.Txn) error {
		e := badger.NewEntry(key, value).WithTTL(time.Hour * 48)
		err := txn.SetEntry(e)
		return err
	})
	return err
}

func DeleteKeyValue(key []byte) error {
	err := badgerDB.Update(func(txn *badger.Txn) error {
		err := txn.Delete(key)
		return err
	})
	return err
}

func GetNKeyValues(numberOfKeys int64) ([][]byte, [][]byte, error) { // key, value, error
	var outputKeySlice, outputValuesSlice [][]byte
	err := badgerDB.View(func(txn *badger.Txn) error {
		opts := badger.DefaultIteratorOptions
		opts.PrefetchValues = true
		opts.PrefetchSize = int(numberOfKeys)
		it := txn.NewIterator(opts)
		defer it.Close()
		var keyCount int64 = 0
		for it.Rewind(); it.Valid() && keyCount < numberOfKeys; it.Next() {
			item := it.Item()
			if item.IsDeletedOrExpired() {
				continue
			}
			keyCount++
			outputKeySlice = append(outputKeySlice, item.Key())
			err := item.Value(func(v []byte) error {
				value := make([]byte, len(v))
				copy(value, v)
				outputValuesSlice = append(outputValuesSlice, value)
				return nil
			})
			if err != nil {
				return err
			}
		}
		return nil
	})
	return outputKeySlice, outputValuesSlice, err
}

```

### What did you expect to see?

I expected to only get non-deleted key value from the GetNKeyValues function

### What did you see instead?

The deleted keys pair are being returned and not sure what am I missing here.

---

<div class="post-metadata">

**Author:** ![debasish-mihup](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/debasish-mihup/32/8606_2.png) [@debasish-mihup](https://discuss.dgraph.io/u/debasish-mihup)\
**Post date:** [July 21, 2021, 12:54pm UTC](https://discuss.dgraph.io/t/db-returning-deleted-keys-value-pair/15013/2 "2021-07-21T12:54:04Z")

</div>

There is some weird behaviour that I am seeing while using badger db. I inserted a key value pair. Key is as below:

`051eb637-a87c-4e9d-9c76-ace44f2998df_i_10_20210703-080017_20210703-080057_20210703-081123_6260308211_Answered-Completed_11299697_13154808.wav`

And while iterating through the db I found this key which is OK but also I found another key

`051eb637-a87c-4e9d-9c76-ace44f2998df_i_10_20210703-080017_20210703-080057_20210703-081123_6260308211_Answered-Completed_11299697_13154808.wavv`

which I never inserted (there is an additional “v” at the end). Also, even if I delete this key post processing, I still receive this key during next iteration. Can not seem to understand how come this error can happen on such basic operation?

---

<div class="post-metadata">

**Author:** ![debasish-mihup](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/debasish-mihup/32/8606_2.png) [@debasish-mihup](https://discuss.dgraph.io/u/debasish-mihup)\
**Post date:** [July 22, 2021, 5:49am UTC](https://discuss.dgraph.io/t/db-returning-deleted-keys-value-pair/15013/3 "2021-07-22T05:49:41Z")

</div>

Replaced badgerDB with levelDB and did not face any of these problem anymore. Maybe deprecating github issue was a bad idea as I don’t find anyone here giving any replies.
