# ZPOPMAX

> Remove and return the members with the highest scores from a sorted set.

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

```
ZPOPMAX key [count]
```

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

`ZPOPMAX` removes and returns the member with the highest score from the
sorted set at `key`, together with its score. With `count`, it removes and
returns up to `count` members, highest score first. When the sorted set
becomes empty, the key is deleted.

To wait for a member when the set is empty, use [BZPOPMAX](https://docs.hotkv.com/commands/bzpopmax/index.md).

## Return value

| Case | RESP2 | RESP3 |
| --- | --- | --- |
| No `count` | Two-element array: member, score (bulk string) | Two-element array: member, score (double) |
| With `count` | Flat array: member, score, member, score, ... | Array of two-element arrays: `[member, score]`, the score a double |
| Key does not exist, or `count` is `0` | Empty array | Empty array |

Errors:

- `ERR value is out of range, must be positive` if `count` is negative or not
  an integer. This is checked before the key is looked up.
- `ERR syntax error` if more than one `count` argument is given.
- `WRONGTYPE` if `key` holds a value that is not a sorted set.

## Examples

```
127.0.0.1:6379> ZADD board 1 "a" 2 "b" 3 "c"
(integer) 3
127.0.0.1:6379> ZPOPMAX board
1) "c"
2) "3"
127.0.0.1:6379> ZPOPMAX board 2
1) "b"
2) "2"
3) "a"
4) "1"
```

With RESP3 (`redis-cli -3`) and a count, each pair is nested:

```
127.0.0.1:6379> ZADD board 1 "a" 2 "b"
(integer) 2
127.0.0.1:6379> ZPOPMAX board 2
1) 1) "b"
   2) (double) 2
2) 1) "a"
   2) (double) 1
```

## Compatibility

Same as Redis 7.4 and Valkey 8.

## See also

[ZPOPMIN](https://docs.hotkv.com/commands/zpopmin/index.md), [BZPOPMAX](https://docs.hotkv.com/commands/bzpopmax/index.md), [ZMPOP](https://docs.hotkv.com/commands/zmpop/index.md),
[ZREVRANGE](https://docs.hotkv.com/commands/zrevrange/index.md)