Skip to content

Ask the HotKV docs

Answers come only from these docs and can be wrong; check the linked pages before you rely on them.

SPOP

Last updated
SPOP key [count]
Group
Set
Complexity
O(1) without count, otherwise O(N) where N is count
Arity
-2 (at least 2 words, counting the command name)
Flags
write, fast
Keys
first 1, last 1, step 1
HotKV Cloud
All plans
On-Premise
no license needed

SPOP removes a random member from the set stored at key and returns it. With count, it removes and returns up to count distinct random members; if the set has fewer, all of them are returned. When the set becomes empty, the key is deleted.

To read random members without removing them, use SRANDMEMBER. Replicas and the append-only file record the members that were actually removed, so they stay identical to the primary.

Case RESP2 RESP3
No count, set exists Bulk string: the removed member Same
No count, key does not exist Nil bulk string Null
With count Array of the removed members (empty if the key does not exist or count is 0) Set of the removed members

Errors:

  • ERR value is out of range, must be positive if count is negative or not an integer. This is checked before the key is looked up.
  • ERR syntax error if more than one count argument is given.
  • WRONGTYPE if key holds a value that is not a set.
127.0.0.1:6379> SADD raffle "ann" "bob" "cat" "dan"
(integer) 4
127.0.0.1:6379> SPOP raffle
"cat"
127.0.0.1:6379> SPOP raffle 2
1) "dan"
2) "ann"
127.0.0.1:6379> SCARD raffle
(integer) 1

The members you get back are random, so your output will differ.

Same as Redis 7.4 and Valkey 8.

SRANDMEMBER, SREM, SMEMBERS