# ZMPOP

> Pop the lowest or highest scored members from the first non-empty sorted set among several keys.

URL: https://docs.hotkv.com/commands/zmpop/

```
ZMPOP numkeys key [key ...] MIN | MAX [COUNT count]
```

- Group: Sorted set
- Complexity: O(K)+O(M*log(N)) where K is the number of keys checked, N the size of the sorted set and M the number of members popped
- HotKV Cloud: all plans
- On-Premise: no license needed

`ZMPOP` checks the given keys in order and pops from the first one that holds
a non-empty sorted set. It removes up to `count` members (default `1`), all
from that one sorted set: the lowest scores with `MIN`, the highest with `MAX`.
The key is deleted when it becomes empty.

To wait when every sorted set is empty, use [BZMPOP](https://docs.hotkv.com/commands/bzmpop/index.md).

## Options

| Option | Effect |
| --- | --- |
| `numkeys` | How many keys follow. Must be greater than 0. |
| `MIN` | Pop the members with the lowest scores. |
| `MAX` | Pop the members with the highest scores. |
| `COUNT count` | Pop up to `count` members. Must be greater than 0. Can be given once. |

## Return value

| Case | RESP2 | RESP3 |
| --- | --- | --- |
| Popped | Two-element array: the key, then an array of `[member, score]` pairs (score as a bulk string) | Same shape, with each score a double |
| All keys missing or empty | Nil array | Null |

The pairs are nested under both protocols.

Errors:

- `ERR numkeys should be greater than 0` if `numkeys` is not a positive
  integer.
- `ERR syntax error` if fewer keys follow than `numkeys` says, the direction
  is missing or not `MIN`/`MAX`, or `COUNT` is repeated or unknown words
  follow.
- `ERR count should be greater than 0` if `count` is not a positive integer.
- `WRONGTYPE` if a key reached before the first non-empty sorted set holds a
  value that is not a sorted set (a plain set counts as the wrong type here).

## Examples

```
127.0.0.1:6379> ZADD tasks 3 "low" 1 "urgent" 2 "normal"
(integer) 3
127.0.0.1:6379> ZMPOP 2 nosuchkey tasks MIN COUNT 2
1) "tasks"
2) 1) 1) "urgent"
      2) "1"
   2) 1) "normal"
      2) "2"
127.0.0.1:6379> ZMPOP 1 nosuchkey MAX
(nil)
```

## Compatibility

Same as Redis 7.4 and Valkey 8.

## See also

[BZMPOP](https://docs.hotkv.com/commands/bzmpop/index.md), [ZPOPMIN](https://docs.hotkv.com/commands/zpopmin/index.md), [ZPOPMAX](https://docs.hotkv.com/commands/zpopmax/index.md),
[LMPOP](https://docs.hotkv.com/commands/lmpop/index.md)