w3resource

Redis Sorted Sets: ZREMRANGEBYRANK

Redis ZREMRANGEBYRANK Command

Redis ZREMRANGEBYRANK command is used to remove all elements in the sorted set stored at the key with the rank between start and stop.

Both start and stop are zero-based indexes,

  • 0 is the first element,
  • 1 is the next element and so on.

They can also be negative numbers indicating offsets starting at the element with the highest score.

  • -1 being the last element of the sorted set
  • -2 the penultimate element and so on.

Here are some common uses and scenarios for the "ZREMRANGEBYRANK" command:

  • Rank-Based Removal: Remove members from a sorted set within a specified rank range.
  • Leaderboards: Manage leaderboards by removing players within a specific ranking range.
  • Data Management: Clean up and manage data by removing members based on their positions.
  • Performance Optimization: Optimize memory usage by removing elements based on their positions in sorted sets.
  • Data Integrity: Ensure data integrity by removing elements that fall within undesired rank ranges.

Syntax:

ZREMRANGEBYRANK key start stop

Available since

2.0.0.

Return Value

Integer reply, the number of elements removed.

Return Value Type

Integer

Example: Redis ZREMRANGEBYRANK: Deleting a member to the index range

127.0.0.1:6379> ZADD mycityset 1 Delhi 2 Mumbai 3 Hyderabad 4 Kolkata 5 Chennai
(integer) 5
127.0.0.1:6379> ZRANGE mycityset 0 -1 WITHSCORES
 1) "Delhi"
 2) "1"
 3) "Mumbai"
 4) "2"
 5) "Hyderabad"
 6) "3"
 7) "Kolkata"
 8) "4"
 9) "Chennai"
10) "5"
127.0.0.1:6379> ZREMRANGEBYRANK mycityset 1 2
(integer) 2
127.0.0.1:6379> ZRANGE mycityset 0 -1 WITHSCORES
1) "Delhi"
2) "1"
3) "Kolkata"
4) "4"
5) "Chennai"
6) "5"

Example: Redis ZREMRANGEBYRANK: Deleting a member using a negative index

127.0.0.1:6379> ZADD mycityset 1 Delhi 2 Mumbai 3 Hyderabad 4 Kolkata 5 Chennai
(integer) 5
127.0.0.1:6379> ZRANGE mycityset 0 -1 WITHSCORES
 1) "Delhi"
 2) "1"
 3) "Mumbai"
 4) "2"
 5) "Hyderabad"
 6) "3"
 7) "Kolkata"
 8) "4"
 9) "Chennai"
10) "5"
127.0.0.1:6379> ZREMRANGEBYRANK mycityset -3 -2
(integer) 2
127.0.0.1:6379> ZRANGE mycityset 0 -1 WITHSCORES
1) "Delhi"
2) "1"
3) "Mumbai"
4) "2"
5) "Chennai"
6) "5"

Previous: ZREMRANGEBYLEX
Next: ZREMRANGEBYSCORE



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/redis/redis-zremrangebyrank-key-start-stop.php