Use LINSERT to insert an element immediately before or after another element of a list.
The pivot is matched by value, and only its first occurrence starting from the head is used. The reply is the new length of the list, 0 when the key does not exist, and -1 when the pivot value was not found, which is how you tell a failed insert from a successful one.
Finding the pivot means scanning the list, so this is a linear operation; on long lists it is worth keeping an index elsewhere or using a sorted set instead.
Syntax#
Arguments#
| Argument | Required | Repeatable | Description |
|---|---|---|---|
<key> | Yes | No | Redis key targeted by the command. |
(BEFORE | AFTER) | Yes | No | Where to place the new element relative to the pivot: BEFORE or AFTER. |
<pivot> | Yes | No | Existing element to insert next to. |
<element> | Yes | No | Element to insert. |
Response#
The reply reports the result of the operation. Error replies have the same shape in RESP2 and RESP3 and are surfaced as exceptions by the SDKs below.
| Protocol | Reply |
|---|---|
| RESP2 | Integer: the list length after insertion, -1 if the pivot was not found, 0 if the key does not exist |
| RESP3 | Integer: the list length after insertion, -1 if the pivot was not found, 0 if the key does not exist |
Client libraries often decode bulk strings, maps, sets, and numeric strings into language-native values. The table describes the Redis wire reply.
Examples#
TCP examples use the TLS REDIS_URL from the Upstash console. REST examples use UPSTASH_REDIS_REST_URL and UPSTASH_REDIS_REST_TOKEN.