How to delete an element from a set in redis – Redis SPOP | SREM

4 days ago Lalit Bhagtani 0

In this tutorial, we will learn about how to delete an element from a set value stored at a key, using redis SPOP and SREM command.

SPOP Command :-

This command, removes and returns one or more random elements from the set stored at the specified key. The syntax of redis SPOP command is as follows :-

Syntax :-

redis host:post> SPOP <key name> [count]

Here count represents, total number of elements to be removed from the set.

Output :-

- (strings reply), representing removed elements from the set.
- (nil), if key does not exists.

Example :-

Redis SPOP

SREM Command :-

This command, deletes one or more specified elements from the set stored at the key. Specified members that are not present in the set are ignored. The syntax of redis SREM command is as follows :-

Syntax :-

redis host:post> SREM <key name 1> <key name 2>

Output :-

- (integer) representing number of elements deleted from the set, excluding non existing members.
- 0 if key does not exist.
- error if key exist and value stored at the key is not a set.

Example :-

Redis SREM

References :-

  1. SPOP Command Docs
  2. SREM Command Docs

That’s all for how to delete an element from a set value stored in redis datastore. If you liked it, please share your thoughts in comments section and share it with others too.