content/commands/hexpire.md
Set an expiration (TTL or time to live) on one or more fields of a given hash key. You must specify at least one field. Field(s) will automatically be deleted from the hash key when their TTLs expire.
Field expirations will only be cleared by commands that delete or overwrite the
contents of the hash fields, including [HDEL]({{< relref "/commands/hdel" >}}) and [HSET]({{< relref "/commands/hset" >}})
commands.
This means that all the operations that conceptually alter the value stored at a hash key's field without replacing it with a new one will leave the TTL untouched.
You can clear the TTL using the [HPERSIST]({{< relref "/commands/hpersist" >}}) command, which turns the hash field back into a persistent field.
Note that calling HEXPIRE/[HPEXPIRE]({{< relref "/commands/hpexpire" >}}) with a zero TTL or
[HEXPIREAT]({{< relref "/commands/hexpireat" >}})/[HPEXPIREAT]({{< relref "/commands/hpexpireat" >}}) with a time in the past will result in the hash field being deleted.
The HEXPIRE command supports a set of options:
NX -- For each specified field, set expiration only when the field has no expiration.XX -- For each specified field, set expiration only when the field has an existing expiration.GT -- For each specified field, set expiration only when the new expiration is greater than current one.LT -- For each specified field, set expiration only when the new expiration is less than current one.A non-volatile field is treated as an infinite TTL for the purpose of GT and LT.
The NX, XX, GT, and LT options are mutually exclusive.
You can call HEXPIRE using as argument a field that already has an
existing TTL set.
In this case, the time to live is updated to the new value.
Starting with Redis 8, Redis Search has enhanced behavior when handling expiring hash fields. For detailed information about how [FT.SEARCH]({{< relref "/commands/ft.search" >}}) and [FT.AGGREGATE]({{< relref "/commands/ft.aggregate" >}}) commands interact with expiring hash fields, see [Key and field expiration behavior]({{< relref "/develop/ai/search-and-query/advanced-concepts/expiration" >}}).
{{< clients-example set="cmds_hash" step="hexpire" description="Field expiration: Set TTL on individual hash fields using HEXPIRE with conditional options (NX, XX, GT, LT) when you need fine-grained control over field lifecycle" difficulty="intermediate" >}} HEXPIRE no-key 20 NX FIELDS 2 field1 field2 (nil) HSET mykey field1 "hello" field2 "world" (integer) 2 HEXPIRE mykey 10 FIELDS 3 field1 field2 field3
Give these commands a try in the interactive console:
{{% redis-cli %}} HEXPIRE no-key 20 NX FIELDS 2 field1 field2 HSET mykey field1 "hello" field2 "world" HEXPIRE mykey 10 FIELDS 3 field1 field2 field3 HGETALL mykey {{% /redis-cli %}}
| Redis Software | Redis Cloud | <span style="min-width: 9em; display: table-cell">Notes</span> | |:----------------------|:-----------------|:------| | <span title="Supported">✅ Standard</span> <span title="Supported"><nobr>✅ Active-Active</nobr></span> | <span title="Supported">✅ Standard</span> <span title="Supported"><nobr>✅ Active-Active</nobr></span> | |
{{< multitabs id="hexpire-return-info" tab1="RESP2" tab2="RESP3" >}}
One of the following:
-2 if no such field exists in the provided hash key, or the provided key does not exist.0 if the specified NX | XX | GT | LT condition has not been met.1 if the expiration time was set/updated.2 when HEXPIRE/HPEXPIRE is called with 0 seconds/milliseconds or when HEXPIREAT/HPEXPIREAT is called with a past Unix time in seconds/milliseconds.-tab-sep-
One of the following:
-2 if no such field exists in the provided hash key, or the provided key does not exist.0 if the specified NX | XX | GT | LT condition has not been met.1 if the expiration time was set/updated.2 when HEXPIRE/HPEXPIRE is called with 0 seconds/milliseconds or when HEXPIREAT/HPEXPIREAT is called with a past Unix time in seconds/milliseconds.{{< /multitabs >}}