# HPEXPIRE

> Set a time to live in milliseconds on one or more hash fields.

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

```
HPEXPIRE key milliseconds [NX | XX | GT | LT] 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

`HPEXPIRE` works like [HEXPIRE](https://docs.hotkv.com/commands/hexpire/index.md), but the time to live is
given in milliseconds. Each listed field of the hash at `key` is removed when
its time passes, and the key is deleted when its last field goes.

A value of `0` removes the fields at once (result code `2`).

## Options

| Option | Effect |
| --- | --- |
| `NX` | Set the TTL only on fields that have none. |
| `XX` | Set the TTL only on fields that already have one. |
| `GT` | Set the TTL only if it is later than the field's current one. A field without a TTL never matches. |
| `LT` | Set the TTL only if it is earlier than the field's current one. A field without a TTL always matches. |

## Return value

An array with one integer per field, in the order given, under both RESP2 and
RESP3:

| Code | Meaning |
| --- | --- |
| `-2` | The field or the key does not exist. |
| `0` | The condition was not met; nothing changed. |
| `1` | The time to live was set. |
| `2` | The time was `0` or already in the past, so the field was deleted. |

The errors are the same as for [HEXPIRE](https://docs.hotkv.com/commands/hexpire/index.md), with the command
named `hpexpire` in ``ERR invalid expire time in 'hpexpire' command``.

## Examples

```
127.0.0.1:6379> HSET cache:7 body "<html>" etag "x1"
(integer) 2
127.0.0.1:6379> HPEXPIRE cache:7 1500 FIELDS 1 body
1) (integer) 1
127.0.0.1:6379> HPTTL cache:7 FIELDS 2 body etag
1) (integer) 1500
2) (integer) -1
```

## Compatibility

Same arguments and per-field result codes as Redis 7.4. Valkey 8 does not have
hash field expiration.

## See also

[HEXPIRE](https://docs.hotkv.com/commands/hexpire/index.md), [HPEXPIREAT](https://docs.hotkv.com/commands/hpexpireat/index.md), [HPTTL](https://docs.hotkv.com/commands/hpttl/index.md),
[HPERSIST](https://docs.hotkv.com/commands/hpersist/index.md), [PEXPIRE](https://docs.hotkv.com/commands/pexpire/index.md)