# Drop/Delete Range of Keys

**URL:** <https://discuss.dgraph.io/t/drop-delete-range-of-keys/8723>\
**Category:** Issues\
**Tags:** badger, status:accepted, area:performance, area:api, skip:stale\
**Created:** [July 7, 2019, 4:18pm UTC](https://discuss.dgraph.io/t/drop-delete-range-of-keys/8723 "2019-07-07T16:18:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![diggy](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/diggy/32/3666_2.png) [@diggy](https://discuss.dgraph.io/u/diggy)\
**Post date:** [July 7, 2019, 4:18pm UTC](https://discuss.dgraph.io/t/drop-delete-range-of-keys/8723/1 "2019-07-07T16:18:08Z")

</div>

**Moved from GitHub [badger/922](https://github.com/dgraph-io/badger/issues/922)**

_Posted by_ [256dpi](https://github.com/256dpi):

**TL;DR**

It would be wonderful if badger would support some sort of optimized way to handle deletion of a range of keys. Inspiration can be taken from other projects like RocksDB which implements a special range delete tombstone: [DeleteRange: A New Native RocksDB Operation | RocksDB](https://rocksdb.org/blog/2018/11/21/delete-range.html).

**Use Case**

I’m currently working on [quasar](https://github.com/256dpi/quasar) which builds on badger to implement a persistent queue. There is a small runnable example here: [quasar/example/main.go at master · 256dpi/quasar · GitHub](https://github.com/256dpi/quasar/blob/master/example/main.go). The whole thing works pretty well and in a basic setup with one producer and one consumer the steady throughput is about 100’000 messages/second on my computer. I think with badger not being necessarily optimized for that workload it’s pretty neat! 👍

Since the queue has a fixed size, there is a “cleaner” process that periodically deletes processed messages to free space in the queue. This process iterates over the keys and removes them in using one or two transactions: [quasar/ledger.go at master · 256dpi/quasar · GitHub](https://github.com/256dpi/quasar/blob/master/ledger.go#L409).

Unfortunately, this approach is very expensive and is responsible for up to 30% of the CPU time used by the program:

 ![flame-graph](https://canada1.discourse-cdn.com/flex007/uploads/dgraph/original/2X/2/2183bdb070c0a22c9938f65abcabecf01905f783.png)

_The flame graph incorrectly attributes all `badger.(*Iterator).prefetch` calls to the initial `badger.(*Iterator).Seek` call. The normal graph does not make that connection and I assume they also come from the subsequent `badger.(*Iterator).Next()` calls._

With the support of something similar to range delete tombstones in RocksDB this would be possibly a lot faster. Also I would favor an approach that is not externally as for example `db.DropPrefix` is now. In my use case writes and deletes can happen at the same time and thus I would not want to block the database for cleanings.

I imagine an interface like the following:

```go
func (txn *Txn) DeleteRange(from, to []byte) error {}

```

The same functionality could potentially also be used to delete based on a prefix:

```go
func (txn *Txn) DeletePrefix(prefix []byte) error {
  return txn.DeleteRange(prefixStart(prefix), prefixEnd(prefix))
}

```

I didn’t to any research yet on how this could be implemented in badger. Maybe somebody from the team has already thought about this. Anyway, thanks for considering this!

---

<div class="post-metadata">

**Author:** ![diggy](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/diggy/32/3666_2.png) [@diggy](https://discuss.dgraph.io/u/diggy)\
**Post date:** [August 13, 2019, 11:11am UTC](https://discuss.dgraph.io/t/drop-delete-range-of-keys/8723/2 "2019-08-13T11:11:05Z")

</div>

[vgtom](https://github.com/vgtom) _commented_ :

Which parts of the code would i need to look to get started on this??

---

<div class="post-metadata">

**Author:** ![diggy](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/diggy/32/3666_2.png) [@diggy](https://discuss.dgraph.io/u/diggy)\
**Post date:** [February 5, 2020, 8:26pm UTC](https://discuss.dgraph.io/t/drop-delete-range-of-keys/8723/3 "2020-02-05T20:26:51Z")

</div>

[stale](https://github.com/apps/stale) _commented_ :

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

---

<div class="post-metadata">

**Author:** ![diggy](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/diggy/32/3666_2.png) [@diggy](https://discuss.dgraph.io/u/diggy)\
**Post date:** [April 20, 2020, 3:49pm UTC](https://discuss.dgraph.io/t/drop-delete-range-of-keys/8723/4 "2020-04-20T15:49:51Z")

</div>

[jarifibrahim](https://github.com/jarifibrahim) _commented_ :

This looks like a useful improvement.
