SPOP
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.
Return value
Section titled “Return value”| 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 positiveifcountis negative or not an integer. This is checked before the key is looked up.ERR syntax errorif more than onecountargument is given.WRONGTYPEifkeyholds a value that is not a set.
Examples
Section titled “Examples”127.0.0.1:6379> SADD raffle "ann" "bob" "cat" "dan"(integer) 4127.0.0.1:6379> SPOP raffle"cat"127.0.0.1:6379> SPOP raffle 21) "dan"2) "ann"127.0.0.1:6379> SCARD raffle(integer) 1The members you get back are random, so your output will differ.
Compatibility
Section titled “Compatibility”Same as Redis 7.4 and Valkey 8.
See also
Section titled “See also”Was this page helpful?
Thanks for the feedback.