SPOP(3valkey)

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

πŸ’… 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:

RESP3

one of the following:

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 the count argument. 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 πŸ’œ


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