# LMPOP

> Pop from the first non-empty list.

Use `LMPOP` to pop elements from the first of several lists that is not empty.

`<numkeys>` states how many keys follow, `LEFT` or `RIGHT` chooses the end to pop from, and `COUNT` sets how many elements to take, defaulting to one. Keys are examined in the order given and only the first non-empty one is touched, which is exactly what a priority queue needs: list the high priority queue first and it is drained before the others are looked at.

The reply names the key that was popped from together with the elements, so a caller working with several queues knows where the work came from. When every key is empty the reply is null; use [`BLMPOP`](/redis/commands/list/blmpop) to wait instead.

## Syntax

```redis
LMPOP <numkeys> <key> [<key> ...] (LEFT | RIGHT) [COUNT <count>]
```

## Arguments

| Argument | Required | Repeatable | Description |
| --- | --- | --- | --- |
| `<numkeys>` | Yes | No | Number of key arguments that follow. |
| `<key>` | Yes | Yes | Redis key targeted by the command. |
| `(LEFT \| RIGHT)` | Yes | No | Which end to pop from: `LEFT` (head) or `RIGHT` (tail). |
| `COUNT <count>` | No | No | Maximum number of elements to pop. |

## 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 | Null bulk string or null array, or two-element array: key and array of values |
| RESP3 | Null, or two-element array: key and array of values |

<Note>
  Client libraries often decode bulk strings, maps, sets, and numeric strings into language-native values. The table describes the Redis wire reply.
</Note>

## Examples

TCP examples use the TLS `REDIS_URL` from the Upstash console. REST examples use `UPSTASH_REDIS_REST_URL` and `UPSTASH_REDIS_REST_TOKEN`.

<AccordionGroup>

<Accordion title="Redis CLI" icon="terminal">

```bash
LMPOP 1 my-key LEFT
```

</Accordion>

<Accordion title="@upstash/redis" icon="node-js" iconType="brands">

```ts
import { Redis } from "@upstash/redis";

const redis = Redis.fromEnv();
const result = await redis.lmpop(1, ["my-key"], "LEFT");
console.log(result);
```

</Accordion>

<Accordion title="upstash_redis" icon="python" iconType="brands">

<Note>
  This command is not supported yet in `upstash_redis`.
</Note>

</Accordion>

<Accordion title="ioredis" icon="node-js" iconType="brands">

```ts
import Redis from "ioredis";

const redis = new Redis(process.env.REDIS_URL!);
const result = await redis.lmpop("1", "my-key", "LEFT");
console.log(result);
```

</Accordion>

<Accordion title="node-redis" icon="node-js" iconType="brands">

```ts
import { createClient } from "redis";

const client = await createClient({ url: process.env.REDIS_URL })
  .on("error", console.error)
  .connect();
const result = await client.lmPop("my-key", "LEFT");
console.log(result);
```

</Accordion>

<Accordion title="redis-py" icon="python" iconType="brands">

```python
import os
import redis

client = redis.from_url(os.environ["REDIS_URL"])
result = client.lmpop(1, "my-key", direction="LEFT")
print(result)
```

</Accordion>

<Accordion title="go-redis" icon="golang" iconType="brands">

```go
package main

import (
    "context"
    "fmt"
    "os"

    "github.com/redis/go-redis/v9"
)

func main() {
    opts, err := redis.ParseURL(os.Getenv("REDIS_URL"))
    if err != nil {
        panic(err)
    }
    client := redis.NewClient(opts)
    _, result, err := client.LMPop(context.Background(), "LEFT", 0, "my-key").Result()
    if err != nil {
        panic(err)
    }
    fmt.Println(result)
}
```

</Accordion>

<Accordion title="jedis" icon="java" iconType="brands">

```java
import java.net.URI;

import redis.clients.jedis.Jedis;

try (Jedis jedis = new Jedis(new URI(System.getenv("REDIS_URL")))) {
  Object result = jedis.lmpop(redis.clients.jedis.args.ListDirection.LEFT, "my-key");
  System.out.println(result);
}
```

</Accordion>

<Accordion title="redis-rs" icon="rust" iconType="brands">

```rust
use redis::{Direction, TypedCommands};

fn main() -> redis::RedisResult<()> {
    let url = std::env::var("REDIS_URL").expect("REDIS_URL is not set");
    let client = redis::Client::open(url)?;
    let mut connection = client.get_connection()?;

    let result = connection.lmpop(1, "my-key", Direction::Left, 1)?;
    println!("{result:?}");
    Ok(())
}
```

</Accordion>

</AccordionGroup>
