Skip to content

Ask the HotKV docs

Answers come only from these docs and can be wrong; check the linked pages before you rely on them.

HPEXPIRE

Last updated
HPEXPIRE key milliseconds [NX | XX | GT | LT] FIELDS numfields field [field ...]
Group
Hash
Complexity
O(N) where N is the number of fields given
Arity
-6 (at least 6 words, counting the command name)
Flags
write, fast
Keys
first 1, last 1, step 1
HotKV Cloud
All plans
On-Premise
no license needed

HPEXPIRE works like HEXPIRE, 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).

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.

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, with the command named hpexpire in ERR invalid expire time in 'hpexpire' command.

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

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

HEXPIRE, HPEXPIREAT, HPTTL, HPERSIST, PEXPIRE