# ZMPOP

> Pop from the first non-empty sorted set.

Use `ZMPOP` to pop members from the first of several sorted sets that is not empty.

`<numkeys>` states how many keys follow, `MIN` or `MAX` chooses whether the lowest or the highest scoring members are taken, and `COUNT` sets how many to pop. Keys are examined in the order given and only the first non-empty one is touched, so listing a high priority queue first drains it before the others are considered.

The reply names the key that was popped from along with the members and their scores, which matters when a worker watches several queues. When every key is empty the reply is null; use [`BZMPOP`](/redis/commands/sorted-set/bzmpop) to wait instead of returning.

## Syntax

```redis
ZMPOP <numkeys> <key> [<key> ...] (MIN | MAX) [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. |
| `(MIN \| MAX)` | Yes | No | Which members to pop: `MIN` (lowest scores first) or `MAX` (highest scores first). |
| `COUNT <count>` | No | No | Maximum number of members to pop. |

## Important points

- RESP2 represents floating-point reply values as bulk strings; RESP3 may use native double replies. Client libraries commonly decode either form to a language number.

## 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 member/score pairs |
| RESP3 | Null, or two-element array: key and array of member/score pairs |

<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
ZMPOP 1 my-key MIN
```

</Accordion>

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

<Note>
  This command is not supported yet in `@upstash/redis`.
</Note>

</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.zmpop("1", "my-key", "MIN");
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.zmPop("my-key", "MIN");
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.zmpop(1, ["my-key"], min=True)
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.ZMPop(context.Background(), "min", 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.zmpop(redis.clients.jedis.args.SortedSetOption.MIN, "my-key");
  System.out.println(result);
}
```

</Accordion>

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

```rust
use redis::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.zmpop_min("my-key", 1)?;
    println!("{result:?}");
    Ok(())
}
```

</Accordion>

</AccordionGroup>
