DB returning deleted keys value pair

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?


Does this issue reproduce with the latest master?


Steps to Reproduce the issue

What Badger options were set?

    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 {

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.

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() {
			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.

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


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


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?

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.