SPOP(3valkey)
π SPOP β bestie's guide to yeeting random members out of a set
okay so. this is the spop page from the Valkey Command Manual, and I'm gonna walk you through it like we're texting at 2am. stay with me, it's literally so good.
β¨ NAME β¨
spop β returns one or more random members from a set after removing them. and bestie, if you pop the last member? the set itself gets deleted. gone. deleted. no cap. π
πΈ SYNOPSIS πΈ
SPOP key [count]
that's it. that's the whole vibe. a key, and optionally a count. π
π¦ DESCRIPTION π¦
so what it does: it removes and returns one or more random members from the set value store living at key. it takes them OUT. they do not come back. it's giving "you can't sit with us" but make it data structures. π
this operation is similar to SRANDMEMBER, except that one returns one or more random elements from a set but does not remove it. so SRANDMEMBER is the friend who borrows your hoodie and gives it back, and SPOP is the friend who moves to another city with it. π
by default, the command pops a single member from the set. when you hand it the optional count argument, the reply will consist of up to count members, depending on the set's cardinality. up to, bestie β not a promise. read the room. β¨
π REPLY π
RESP2
one of the following, bestie:
- valkey-protocol(7) Nil reply: if the key does not exist. π
- valkey-protocol(7) Bulk string reply: when called without the
countargument, the removed member. - valkey-protocol(7) Array reply: when called with the
countargument, a list of the removed members.
RESP3
one of the following:
- valkey-protocol(7) Null reply: if the key does not exist. π
- valkey-protocol(7) Bulk string reply: when called without the
countargument, the removed member. - valkey-protocol(7) Set reply: when called with the
countargument, the set of removed members.
note the glow-up between protocols okay: RESP2 gives you an Array, RESP3 gives you a Set, and nil becomes null. same energy, different fit. π
π₯ COMPLEXITY π₯
without the count argument it's O(1) β instant, effortless, main character energy. otherwise it's O(N), where N is the value of the passed count. so like, the more you ask for, the more work it is. obviously. slay but responsibly. π
π ACL CATEGORIES π
@fast @set @write
π HISTORY π
- available since:
1.0.0β an icon since day one, fr fr. - changed in
3.2.0: added thecountargument. the glow-up era. β¨
β EXAMPLES β
here's the whole thing happening live, watch:
127.0.0.1:6379> SADD myset "one"
(integer) 1
127.0.0.1:6379> SADD myset "two"
(integer) 1
127.0.0.1:6379> SADD myset "three"
(integer) 1
127.0.0.1:6379> SPOP myset
"three"
127.0.0.1:6379> SMEMBERS myset
1) "one"
2) "two"
127.0.0.1:6379> SADD myset "four"
(integer) 1
127.0.0.1:6379> SADD myset "five"
(integer) 1
127.0.0.1:6379> SPOP myset 3
1) "one"
2) "four"
3) "five"
127.0.0.1:6379> SMEMBERS myset
1) "two"
see how "three" got popped and then SMEMBERS is like "who? never heard of her"?? iconic behaviour. then we add "four" and "five", pop three at once, and "two" is the last one standing. main character. π«Ά
Distribution of returned elements
okay important, don't skip this part. note that this command is not suitable when you need a guaranteed uniform distribution of the returned elements. so if you need that, this is not your girl. π for more information about the algorithms used for SPOP, look up both the Knuth sampling</> and Floyd sampling</> algorithms. iykyk. π§
π SEE ALSO π
sadd(3valkey)scard(3valkey)sdiff(3valkey)sdiffstore(3valkey)sinter(3valkey)sintercard(3valkey)sinterstore(3valkey)sismember(3valkey)smembers(3valkey)smismember(3valkey)smove(3valkey)srandmember(3valkey)srem(3valkey)sscan(3valkey)sunion(3valkey)sunionstore(3valkey)
and that's the whole page bestie!! this was version 9.0.0, dated 2025-10-21, from the Valkey command manual β go forth and pop responsibly, mwah ππΈβ¨
π¦ 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