ZREMRANGEBYRANK(3valkey)
β¨ ZREMRANGEBYRANK π (from the valkey command manual, section 3valkey)
okay bestie, gather round, because this one is a glow-up command. it literally deletes a whole chunk of your sorted set based on rank, and if it clears the whole thing out? the key itself is gone. bye. no cap. π«Ά
πΈ NAME πΈ
zremrangebyrank β removes members in a sorted set within a range of indexes. deletes the sorted set if all members were removed. periodt. π
π SYNOPSIS π
ZREMRANGEBYRANK key start stop
π¦ DESCRIPTION π¦
so like. this command removes all the elements in the sorted set stored at key whose rank is between start and stop. iconic behavior honestly.
both start and stop are 0-based indexes, and 0 is the element with the lowest score. (yes, zero. lowest. not one. we're programmers here π
)
and here's the fun part: these indexes can be negative numbers! when they're negative, they indicate offsets starting at the element with the highest score. so:
-1is the element with the highest score π-2is the element with the second highest score- and so forth, you get the vibe, it just keeps going down the leaderboard
π REPLY π
valkey-protocol(7) Integer reply: the number of members removed. that's it, that's the tea. π₯
β‘ COMPLEXITY β‘
O(log(N)+M) β where N is the number of elements in the sorted set, and M is the number of elements removed by the operation. so like, the more you delete, the more it costs. math is math. π
π ACL CATEGORIES π
@slow @sortedset @write
π HISTORY π
- available since:
2.0.0β an OG, fr fr β
π EXAMPLES π
watch and learn bestie:
127.0.0.1:6379> ZADD myzset 1 "one"
(integer) 1
127.0.0.1:6379> ZADD myzset 2 "two"
(integer) 1
127.0.0.1:6379> ZADD myzset 3 "three"
(integer) 1
127.0.0.1:6379> ZREMRANGEBYRANK myzset 0 1
(integer) 2
127.0.0.1:6379> ZRANGE myzset 0 -1 WITHSCORES
1) "three"
2) "3"
see how "one" and "two" justβ¦ evaporated? "three" is the last one standing, main character energy fr. πβ¨
π§ SEE ALSO π§
bzmpop(3valkey)bzpopmax(3valkey)bzpopmin(3valkey)zadd(3valkey)zcard(3valkey)zcount(3valkey)zdiff(3valkey)zdiffstore(3valkey)zincrby(3valkey)zinter(3valkey)zintercard(3valkey)zinterstore(3valkey)zlexcount(3valkey)zmpop(3valkey)zmscore(3valkey)zpopmax(3valkey)zpopmin(3valkey)zrandmember(3valkey)zrange(3valkey)zrangebylex(3valkey)zrangebyscore(3valkey)zrangestore(3valkey)zrank(3valkey)zrem(3valkey)zremrangebylex(3valkey)zremrangebyscore(3valkey)zrevrange(3valkey)zrevrangebylex(3valkey)zrevrangebyscore(3valkey)zrevrank(3valkey)zscan(3valkey)zscore(3valkey)zunion(3valkey)zunionstore(3valkey)
okay that's the whole page, you ate that π«Ά this was valkey version 9.0.0, dated 2025-10-21. go forth and delete some ranks, bestie. πππ¦
π¦ where this page comes from
- fedora 44: valkey-doc 9.0.3-2.fc44 Β· CC-BY-SA-4.0
- fedora 44: valkey-doc 9.0.6-1.fc44 Β· CC-BY-SA-4.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms