English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Redis Spop Command

Redis Sets (Set)

The Redis Spop command is used to remove one or more random elements from the specified key in the set, and returns the removed elements after removal.

This command is similar to Srandmember Command, but SPOP removes a random element from the set and returns it, while Srandmember only returns a random element without making any changes to the set.

Syntax

Basic syntax of the redis Spop command is as follows:

SPOP  key  [count]

The count parameter in 3.2+ Version available.

Available version

>= 1.0.0

Return value

The randomly removed element. Returns nil when the set does not exist or is empty.

Online Examples

redis>  SADD  myset  "one"
(integer) 1
redis>  SADD  myset  "two"
(integer) 1
redis>  SADD  myset  "three"
(integer) 1
redis>  SPOP  myset
"one"
redis>  SMEMBERS  myset
1)  "three"
2)  "two"
redis>  SADD  myset  "four"
(integer) 1
redis>  SADD  myset  "five"
(integer) 1
redis>  SPOP  myset 3
1)  "five"
2)  "four"
3)  "two"
redis>  SMEMBERS  myset
1)  "three"
redis>

Redis Sets (Set)