Redis ZREMRANGEBYRANK – How to remove elements of sorted set by rank range

5 years ago Lalit Bhagtani 0

In this tutorial, we will learn about how to remove elements of the sorted set value having rank between a specific range. For this, we will use Redis ZREMRANGEBYRANK command.

ZREMRANGEBYRANK Command

This command remove all the elements of the sorted set value, whose ranks are between start and stop arguments.

The start and stop are zero based indexes where 0 means the lowest score element, 1 means the second lowest score element and so on. Negative numbers can also be used to provide an offset starting from the highest score element of the sorted set value, here -1 means the highest score element, -2 means second highest score element and so on.

An error is returned, if the key exists but value stored at the key is not of sorted set datatype.

The syntax of redis ZREMRANGEBYRANK command is as follows :- 

Syntax :-

redis host:post> ZREMRANGEBYRANK <keyname> <start> <stop>

Output :-

- (integer) reply, representing number of removed elements.
- 0, if key does not exists.
- Error, if key exist and value stored at the key is not a sorted set.

Example :-

Redis ZREMRANGEBYRANK

References :-

  1. ZREMRANGEBYRANK Command Docs

That’s all for how to remove elements of the sorted set value having rank between specific range. If you liked it, please share your thoughts in comments section and share it with others too.