HEXPIRE(3valkey)
π HEXPIRE π
okay bestie, so this is the manual page for hexpire from the Valkey Command Manual, section 3valkey. yes it's a hash-field-expiry situation and yes it's iconic. let's get into it π«Ά
β¨ NAME β¨
hexpire β set expiry time on hash fields. that's it, that's the whole thing. a little countdown timer but for one specific field inside your hash. slay π
πΈ SYNOPSIS πΈ
HEXPIRE key seconds [NX | XX | GT | LT] FIELDS numfields field
[field...]
π¦ DESCRIPTION π¦
okay so the HEXPIRE command lets you mess with the expiration time of hash fields that already exist. when you set an expiration time on a hash field, that's literally the thing that decides when the field gets automatically deleted. bye bestie π
not me needing to say this but: if you pass a millisecond time of 0, the field expires immediately and gets deleted. like instantly. gone. no take-backs. π
π OPTIONS π
the HEXPIRE command supports a lil set of options that change up its behavior:
NXβ for each field you specify, it only sets the expiration when that field has no expiration at all. brand new timer energy β¨XXβ for each field you specify, it only sets the expiration when that field already has an expiration. gotta have one to get one, iykyk.GTβ for each field you specify, it only sets the expiration when the new expiration is greater than the current one. only bigger, only better πLTβ for each field you specify, it only sets the expiration when the new expiration is less than the current one. only smaller, only sooner π
π REPLY π
valkey-protocol(7) Array reply: a list of integer codes telling you how setting the expiry went for each field you asked about, in the exact same order you requested them. like a little report card, one line per field π
-2: the field does not exist in the HASH, or the key does not exist. it's giving "who? never met her" π0: theNX|XX|GT|LTcondition you specified has not been met. vibe check failed, sorry babe.1: the expiration time was applied. ate. π₯2: when called with0seconds. instant-delete behavior, special little code for a special little situation π
β COMPLEXITY β
O(N) where N is the number of fields you specified. so like, more fields = more work, obviously, it's giving linear. π§
π ACL CATEGORIES π
@fast @hash @write
π HISTORY π
- Available since:
9.0.0β a modern queen, born in the 9 era β¨
π₯ EXAMPLES π₯
127.0.0.1:6379> HSET myhash f1 v1 f2 v2 f3 v3
(integer) 3
27.0.0.1:6379> HEXPIRE myhash 10 FIELDS 2 f2 f3
1) (integer) 1
2) (integer) 1
127.0.0.1:6379> HTTL myhash FIELDS 3 f1 f2 f3
1) (integer) -1
2) (integer) 8
3) (integer) 8
so we made three fields, slapped a 10 second expiry on f2 and f3 (both said 1, we love that for them π), and then HTTL was like "f1? never had a timer, -1" and "f2 and f3? 8 seconds left bestie" πβ¨ periodt.
π«Ά SEE ALSO π«Ά
hdel(3valkey), hexists(3valkey), hexpireat(3valkey), hexpiretime(3valkey), hget(3valkey), hgetall(3valkey), hgetex(3valkey), hincrby(3valkey), hincrbyfloat(3valkey), hkeys(3valkey), hlen(3valkey), hmget(3valkey), hmset(3valkey), hpersist(3valkey), hpexpire(3valkey), hpexpireat(3valkey), hpexpiretime(3valkey), hpttl(3valkey), hrandfield(3valkey), hscan(3valkey), hset(3valkey), hsetex(3valkey), hsetnx(3valkey), hstrlen(3valkey), httl(3valkey), hvals(3valkey) β the whole hash friend group, go say hi π
and that's the page!! this was Valkey 9.0.0, dated 2025-10-21, from the HEXPIRE(3valkey) page. mwah, go expire some fields responsibly bestie ππβ¨
π¦ where this page comes from
- fedora 44: valkey-doc 9.0.3-2.fc44 Β· CC-BY-SA-4.0
- fedora 44: valkey-doc 9.0.6-1.fc44 Β· CC-BY-SA-4.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms