Skip to content

Ask the HotKV docs

Answers come only from these docs and can be wrong; check the linked pages before you rely on them.

LINSERT

Last updated
LINSERT key BEFORE | AFTER pivot element
Group
List
Complexity
O(N) where N is the number of elements before the pivot
Arity
5 (exactly, counting the command name)
Flags
write
Keys
first 1, last 1, step 1
HotKV Cloud
All plans
On-Premise
no license needed

LINSERT finds the first occurrence of pivot in the list stored at key, scanning from the head, and inserts element just before or just after it. If pivot is not in the list, nothing changes. A missing key is treated as an empty list and is not created.

Option Effect
BEFORE Insert element immediately before pivot.
AFTER Insert element immediately after pivot.
Case RESP2 RESP3
Inserted Integer: the length of the list after the insert Same
pivot not found Integer -1 Same
Key does not exist Integer 0 Same

Errors:

  • ERR syntax error if the second argument is not BEFORE or AFTER.
  • WRONGTYPE if key holds a value that is not a list.
127.0.0.1:6379> RPUSH steps "one" "three"
(integer) 2
127.0.0.1:6379> LINSERT steps BEFORE "three" "two"
(integer) 3
127.0.0.1:6379> LINSERT steps AFTER "nine" "ten"
(integer) -1
127.0.0.1:6379> LRANGE steps 0 -1
1) "one"
2) "two"
3) "three"

Same as Redis 7.4 and Valkey 8.

LSET, LPOS, LREM, LPUSH