# SPOP

> Remove and return one or more random members of a set.

URL: https://docs.hotkv.com/commands/spop/

```
SPOP key [count]
```

- Group: Set
- Complexity: O(1) without count, otherwise O(N) where N is count
- 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](https://docs.hotkv.com/commands/srandmember/index.md). Replicas and the append-only file
record the members that were actually removed, so they stay identical to the
primary.

## 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 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.

## Examples

```
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.

## Compatibility

Same as Redis 7.4 and Valkey 8.

## See also

[SRANDMEMBER](https://docs.hotkv.com/commands/srandmember/index.md), [SREM](https://docs.hotkv.com/commands/srem/index.md), [SMEMBERS](https://docs.hotkv.com/commands/smembers/index.md)