Upstash Documentation

LINSERT

Insert before or after pivot.
2 min read

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#

ArgumentRequiredRepeatableDescription
<key>YesNoRedis key targeted by the command.
(BEFORE | AFTER)YesNoWhere to place the new element relative to the pivot: BEFORE or AFTER.
<pivot>YesNoExisting element to insert next to.
<element>YesNoElement 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.

ProtocolReply
RESP2Integer: the list length after insertion, -1 if the pivot was not found, 0 if the key does not exist
RESP3Integer: the list length after insertion, -1 if the pivot was not found, 0 if the key does not exist
Note

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.

Redis CLI
@upstash/redis
upstash_redis
ioredis
node-redis
redis-py
go-redis
jedis
redis-rs