LMOVEM

LMOVEM source destination <LEFT | RIGHT> <LEFT | RIGHT>
  [<COUNT count | EXACTLY exactly> <OBO | BULK>]
Available since:
Redis Open Source 8.10.0
Time complexity:
O(N) where N is the number of elements moved.
ACL categories:
@write, @list, @slow,
Compatibility:
Redis Software and Redis Cloud compatibility
Note:
This command's behavior varies in clustered Redis environments. See the multi-key operations page for more information.

Atomically moves one or more elements from the list stored at source to the list stored at destination and returns the moved elements. LMOVEM is the multiple-element version of LMOVE: without the optional how-many block, it behaves exactly like LMOVE but returns the moved element as a one-element array.

Required arguments

source

The key of the source list.

destination

The key of the destination list.

LEFT | RIGHT

The end of source to pop elements from: LEFT (head) or RIGHT (tail).

LEFT | RIGHT

The end of destination to push elements to: LEFT (head) or RIGHT (tail).

Optional arguments

The how-many block controls how many elements are moved and in what order. When you include it, you must provide both a selector (COUNT or EXACTLY) and an ordering (OBO or BULK).

COUNT count | EXACTLY exactly

The number of elements to move:

  • COUNT count moves up to count elements. If source holds fewer than count elements, all of them are moved. This matches the count semantics of LPOP and LMPOP.
  • EXACTLY exactly moves exactly exactly elements. If source holds fewer than exactly elements, nothing is moved and the command returns nil.
OBO | BULK

The order in which elements are pushed onto destination:

  • OBO (one by one) moves elements individually: each element is popped from source and pushed to destination before the next one is moved.
  • BULK moves all of the elements at once, preserving their relative order.

The two orderings differ only in the resulting arrangement of elements. See Element ordering for details.

Examples

> rpush mylist 1 2 3 4
(integer) 4
> LMOVEM mylist myotherlist LEFT LEFT COUNT 2 OBO
1) "2"
2) "1"
> LRANGE mylist 0 -1
1) "3"
2) "4"
> LRANGE myotherlist 0 -1
1) "2"
2) "1"
> LMOVEM mylist myotherlist LEFT LEFT EXACTLY 5 BULK
(nil)
> RPUSH somelist a b c
(integer) 3
> LMOVEM somelist somelist LEFT LEFT EXACTLY 2 OBO
1) "b"
2) "a"
> LRANGE somelist 0 -1
1) "b"
2) "a"
3) "c"

Details

Moving multiple elements

LMOVEM moves elements from one end of source (selected by the first LEFT | RIGHT argument) to one end of destination (selected by the second LEFT | RIGHT argument), and returns the moved elements as an array. If source does not exist, no operation is performed and the command returns an empty array (or nil when EXACTLY is used).

The COUNT and EXACTLY selectors decide how many elements are moved:

  • With COUNT count, the command moves up to count elements. If source holds fewer than count elements, all available elements are moved.
  • With EXACTLY exactly, the command moves the requested number of elements only if source holds at least that many. Otherwise it moves nothing and returns nil. This is useful when elements form fixed-size groups (for example, pairs or triplets) that must be moved together or not at all.

Element ordering

The OBO and BULK orderings determine how the moved elements are arranged on destination:

  • OBO (one by one) pops and pushes each element in turn, so an element popped earlier ends up "beneath" one popped later at the destination end.
  • BULK moves the whole batch at once, preserving the elements' relative order.

When source and destination are the same key, this difference becomes a useful side effect. BULK leaves the list unchanged (the elements are removed and re-added in the same order), so it is effectively a no-op. OBO, however, reverses the moved elements in place. For example, with mylist holding a, b, c, running LMOVEM mylist mylist LEFT LEFT EXACTLY 2 OBO pops a then b and pushes each back to the head, leaving mylist as b, a, c.

Redis Software and Redis Cloud compatibility

Redis
Software
Redis
Cloud
Notes
❌ Standard
❌ Active-Active
❌ Standard
❌ Active-Active

Return information

One of the following:

  • Array reply: the moved elements, in destination order.
  • Nil reply: if EXACTLY was used and source did not hold enough elements.

See also

LMOVE | BLMOVEM | LMPOP | RPOPLPUSH

RATE THIS PAGE
Back to top ↑