The Redis SMOVE command moves the specified member element from the source set to the destination set.

SMOVE operation is an atomic.

If the source set does not exist or does not contain the specified member, the command does nothing and returns 0. Otherwise, the member is removed from the source set and added to the destination set.

If the destination set already contains the member, the SMOVE command simply removes the member from the source set without adding it to the destination set again.

If either the source or the destination is not a set, an error is returned.

Syntax

The basic syntax of the SMOVE command is as follows:

1
SMOVE source destination member

Move member from the set at source to the set at destination.

Available Since

Redis version >= 1.0.0

Time Complexity

O(1)

ACL Categories

@write, @set, @fast

Return Value

  • 1 if the element is moved.
  • 0 if the element is not a member of source and no operation was performed.

Example 1

Moves the specified member element from the source set to the destination set:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
127.0.0.1:6379> SADD myset a b c d
(integer) 4
127.0.0.1:6379> SADD anotherset e
(integer) 1
127.0.0.1:6379> SMOVE myset anotherset b
(integer) 1
127.0.0.1:6379> SMEMBERS myset
1) "c"
2) "a"
3) "d"
127.0.0.1:6379> SMEMBERS anotherset
1) "e"
2) "b"
127.0.0.1:6379>

Example 2

If the source set does not exist, no operation is performed and 0 is returned.

1
2
3
4
5
127.0.0.1:6379> EXISTS myset2
(integer) 0
127.0.0.1:6379> SMOVE myset2 anotherset a
(integer) 0
127.0.0.1:6379>

Example 3

If the source set does not contain the specified element, no operation is performed and 0 is returned.

1
2
3
4
5
6
7
8
9
127.0.0.1:6379> SADD myset a b c d
(integer) 4
127.0.0.1:6379> SADD anotherset e
(integer) 1
127.0.0.1:6379> SMOVE myset anotherset p
(integer) 0
127.0.0.1:6379> SMEMBERS anotherset
1) "e"
127.0.0.1:6379>

Example 4

When the specified element already exists in the destination set, it is only removed from the source set.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
127.0.0.1:6379> SADD myset a b c d
(integer) 4
127.0.0.1:6379> SADD anotherset a e
(integer) 2
127.0.0.1:6379> SMOVE myset anotherset a
(integer) 1
127.0.0.1:6379> SMEMBERS myset
1) "c"
2) "b"
3) "d"
127.0.0.1:6379> SMEMBERS anotherset
1) "e"
2) "a"
127.0.0.1:6379>

Example 5

An error is returned if source or destination does not hold a set value.

1
2
3
4
5
6
7
127.0.0.1:6379> SET dest johnson
OK
127.0.0.1:6379> TYPE dest
string
127.0.0.1:6379> SMOVE myset dest e
(error) WRONGTYPE Operation against a key holding the wrong kind of value
127.0.0.1:6379>