# HGETEX

> Get the values of hash fields and optionally set or remove their time to live.

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

```
HGETEX key [EX seconds | PX milliseconds | EXAT unix-time-seconds | PXAT unix-time-milliseconds | PERSIST] FIELDS numfields field [field ...]
```

- Group: Hash
- Complexity: O(N) where N is the number of fields given
- HotKV Cloud: all plans
- On-Premise: no license needed

`HGETEX` returns the values of the listed fields of the hash at `key`, like
[HMGET](https://docs.hotkv.com/commands/hmget/index.md), and at the same time changes the time to live of
the fields that exist. Without an expiry option it only reads.

The values are read first. If the expiry you give is already in the past, the
fields are returned and then deleted (and the key too, if no fields remain).

## Options

| Option | Effect |
| --- | --- |
| `EX seconds` | Expire the fields after this many seconds. |
| `PX milliseconds` | Expire the fields after this many milliseconds. |
| `EXAT unix-time-seconds` | Expire the fields at this Unix time, in seconds. |
| `PXAT unix-time-milliseconds` | Expire the fields at this Unix time, in milliseconds. |
| `PERSIST` | Remove the fields' time to live. |
| `FIELDS numfields field ...` | The fields to read. Required, exactly `numfields` names. |

Only one of `EX`, `PX`, `EXAT`, `PXAT` and `PERSIST` can be given. The options
and the `FIELDS` block can come in any order after the key. Times must be
non-negative integers.

## Return value

| Case | RESP2 | RESP3 |
| --- | --- | --- |
| Success | Array with one entry per field: the value as a bulk string, or nil bulk string if the field does not exist | Array of bulk strings, with null for a missing field |
| Key does not exist | Array of nils, one per field | Array of nulls |

Errors:

- `WRONGTYPE` if `key` holds a value that is not a hash (checked first).
- `ERR Only one of EX, PX, EXAT, PXAT or PERSIST arguments can be specified`.
- `ERR value is not an integer or out of range` or
  `ERR invalid expire time, must be >= 0` for a bad time.
- `ERR invalid number of fields`, `ERR wrong number of arguments` (fewer names
  than `numfields`), `ERR FIELDS keyword specified multiple times`,
  `ERR missing FIELDS argument`, or `ERR unknown argument: <arg>`.
- ``ERR invalid expire time in 'hgetex' command`` if the time is too far in
  the future.

## Examples

```
127.0.0.1:6379> HSET session:9 token "abc" user "42"
(integer) 2
127.0.0.1:6379> HGETEX session:9 EX 300 FIELDS 2 token nope
1) "abc"
2) (nil)
127.0.0.1:6379> HTTL session:9 FIELDS 1 token
1) (integer) 300
127.0.0.1:6379> HGETEX session:9 PERSIST FIELDS 1 token
1) "abc"
127.0.0.1:6379> HTTL session:9 FIELDS 1 token
1) (integer) -1
```

## Compatibility

Same as Redis 8.0. Redis 7.4 and Valkey 8 do not have this command.

## See also

[HMGET](https://docs.hotkv.com/commands/hmget/index.md), [HSETEX](https://docs.hotkv.com/commands/hsetex/index.md), [HEXPIRE](https://docs.hotkv.com/commands/hexpire/index.md),
[HPERSIST](https://docs.hotkv.com/commands/hpersist/index.md), [HGETDEL](https://docs.hotkv.com/commands/hgetdel/index.md), [GETEX](https://docs.hotkv.com/commands/getex/index.md)