Delete documents does not work (always)

Please see the code snipplet below. I want to iterate over all documents in the database. Then I do something with the value in the handlemessage() function. So far so good. The last part is deleting the processed keys. This -for some reason- does not work ALL THE TIME. If I run the code multiple times, then eventually the database will be empty. But I don’t understand why the delete does not work all the time. (I don’t get an error by the way).

Is there some kind of race condition?

err := badgerDB.Update(func(txn *badger.Txn) error {
	opts := badger.DefaultIteratorOptions
	opts.PrefetchValues = false

	it := txn.NewIterator(opts)
	defer it.Close()

	for it.Rewind(); it.Valid(); it.Next() {
		item := it.Item()
		k := item.Key()

		err := item.Value(func(v []byte) error {
			if err := handleMessage(k, v); err != nil {
				log.Fatal(err)
			}
			return nil
		})

		if err := txn.Delete(k); err != nil {
			log.Fatal(err)
		}

		if err != nil {
			return err
		}

	}

	return nil
})

@rogier what do you mean by delete is not working? Are you able to get any key after performing a delete on it? You can try to read deleted keys using badgerDB.View function, you should get Key not found error.
Deleting keys wont reduce disk space occupied by badger immediately, as it performs delete by appending an entry in storage files. This space will be reclaimed when badger performs garbage collection.