# PEXPIRE

> Set a key's time to live in milliseconds.

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

```
PEXPIRE key milliseconds [NX | XX | GT | LT]
```

- Group: Keys
- Complexity: O(1)
- HotKV Cloud: all plans
- On-Premise: no license needed

`PEXPIRE` works like [EXPIRE](https://docs.hotkv.com/commands/expire/index.md), with the time given in
milliseconds. A time of zero or less deletes the key at once.

## Options

| Option | Effect |
| --- | --- |
| `NX` | Set the time only if the key has none. |
| `XX` | Set the time only if the key already has one. |
| `GT` | Set the time only if it is later than the current one (never for a key without a time to live). |
| `LT` | Set the time only if it is earlier than the current one (always for a key without a time to live). |

`NX` cannot be combined with `XX`, `GT` or `LT`, and `GT` cannot be combined
with `LT`.

## Return value

| Case | RESP2 | RESP3 |
| --- | --- | --- |
| The time was set (or the key was deleted) | Integer `1` | Integer `1` |
| The key does not exist, or an option prevented the change | Integer `0` | Integer `0` |

The error replies are the same as for [EXPIRE](https://docs.hotkv.com/commands/expire/index.md), with
`'pexpire'` in the invalid-time message.

## Examples

```
127.0.0.1:6379> SET lock "owner-7"
OK
127.0.0.1:6379> PEXPIRE lock 1500
(integer) 1
127.0.0.1:6379> PTTL lock
(integer) 1500
```

## Compatibility

Same as Redis 7 and Valkey 8.

## See also

[EXPIRE](https://docs.hotkv.com/commands/expire/index.md), [PEXPIREAT](https://docs.hotkv.com/commands/pexpireat/index.md),
[PTTL](https://docs.hotkv.com/commands/pttl/index.md), [NEXPIRE](https://docs.hotkv.com/commands/nexpire/index.md)