# ZREMRANGEBYRANK

> Remove the members of a sorted set in a range of ranks.

URL: https://docs.hotkv.com/commands/zremrangebyrank/

```
ZREMRANGEBYRANK key start stop
```

- Group: Sorted set
- Complexity: O(log(N)+M) where N is the size of the sorted set and M is the number of members removed
- HotKV Cloud: all plans
- On-Premise: no license needed

`ZREMRANGEBYRANK` removes the members of the sorted set at `key` whose rank
(position from the lowest score, starting at `0`) is between `start` and
`stop`, both included. Negative ranks count from the highest score: `-1` is the
highest-scored member. When the sorted set becomes empty, the key is deleted.

For example, `ZREMRANGEBYRANK key 0 -11` keeps only the 10 highest-scored
members.

## Return value

| Case | RESP2 | RESP3 |
| --- | --- | --- |
| Success | Integer: the number of members removed | Same |
| Key does not exist | Integer `0` | Same |

Errors:

- `ERR value is not an integer or out of range` if `start` or `stop` is not an
  integer. This is checked before the key is looked up.
- `WRONGTYPE` if `key` holds a value that is not a sorted set.

## Examples

```
127.0.0.1:6379> ZADD board 1 "a" 2 "b" 3 "c" 4 "d"
(integer) 4
127.0.0.1:6379> ZREMRANGEBYRANK board 0 -3
(integer) 2
127.0.0.1:6379> ZRANGE board 0 -1
1) "c"
2) "d"
```

## Compatibility

Same as Redis 7.4 and Valkey 8.

## See also

[ZREMRANGEBYSCORE](https://docs.hotkv.com/commands/zremrangebyscore/index.md), [ZREMRANGEBYLEX](https://docs.hotkv.com/commands/zremrangebylex/index.md),
[ZRANGE](https://docs.hotkv.com/commands/zrange/index.md), [ZRANK](https://docs.hotkv.com/commands/zrank/index.md)