ZREMRANGEBYRANK(3valkey)

πŸ“¦
version 9.0.6 Β· 2 versions
, in fedora 44

✨ 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:

  • -1 is the element with the highest score πŸ‘‘
  • -2 is 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 🧚


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