Redis SMOVE – How to move element from one set to another set in redis

5 years ago Lalit Bhagtani 0

In this tutorial, we will learn about how to move an element from one set value to another set value stored in redis datastore. For this we will use a COMMAND – SMOVE in redis-cli.

This command is used to remove the specified element from a set value stored at source key and insert the same specified element into a set value stored at destination key. This operation is atomic, which means at any given moment the specified element will appear to be a member of either source set value or destination set value.

If set value at source key does not exist or does not contain the specified element, then no operation is performed and 0 is returned. If specified element already exists in the set value at destination key, then element is only removed from the source set value.

An error is returned if source or destination key does not hold a set value. The syntax of redis SMOVE command is as follows :-

Syntax :-

redis host:post> SMOVE <source> <destination> <element>

Output :- 

- 1, if the element is moved from source to destination.
- 0, if source key does not exist or element is not a member of source set value.
- Error, if source or destination key does not hold a set value.

Example :-

Redis SMOVE

References :-

  1. SMOVE Command Docs

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