Ray's Knowledge Base
Unlock

Delete exactly one key through a range-delete-only interface

RecipeVerified 27 Sep 2026Holds anywhere
Recipe. When to use it, the steps, and what showed that they work.

When to use#

Use this when a write interface offers delete_range(begin, end) but no single-key delete, for example a write abstraction shared by a RocksDB WriteBatch and an SST writer, and you must remove one key without removing the keys that start with it.

Do not use prefix_end(key) as the end: [key, prefix_end(key)) also covers every longer key that starts with key, such as name.a and name.ab for the key name.a.

Steps#

  1. Build the end key as the key plus one zero byte.
  2. Delete the half-open range [key, key + "\0").
void delete_key(IndexWrite& w, Family family, std::string key) {
    std::string end = key;
    end.push_back('\0');
    w.delete_range(family, key, end);
}

With the default bytewise comparator, key + "\0" is the smallest key that is greater than key. So the range holds only key itself.

Evidence#

  • Used in dftracer-utils records::clear_plugin and clear_path (src/dftracer/utils/index/store/index_write.cpp), where the path-keyed layout stores a file-level value under [prefix][path] with no trailing separator.
  • The plugin-extension tests (drop and rebuild one extension by name, while other names stay) and the full C++ suite (282 tests) passed with it.
  • The reasoning depends on the bytewise comparator. A custom comparator can order keys differently; check it before you use this.

Also found by searching

  • WriteBatch has only DeleteRange(begin, end), deleting key name.a also wiped name.ab and name.abc
  • removing one rocksdb key with a range delete also removes every longer key sharing the prefix
  • how do I delete a single exact key when the write API only supports delete_range
  • sst writer no point delete, prefix_end upper bound deletes too much, what end key for one key only
  • write batch only has delete_range(begin,end), how can I delete a single key without removing keys sharing its prefix
  • RocksDB WriteBatch no single delete, prefix_end deletes name.a and name.ab too, what end key to use
  • how to delete exactly one key via DeleteRange in rocksdb sst writer
  • range tombstone for one key only, smallest key greater than key bytewise, append nul byte