SEXPIRE

SEXPIRE key seconds [NX | XX | GT | LT] MEMBERS nummembers member
  [member ...]
Available since:
Redis Open Source 8.12.0
Time complexity:
O(N) where N is the number of specified members
ACL categories:
@write, @set, @fast,
Compatibility:
Redis Software and Redis Cloud compatibility

Specify an expiration (TTL or time to live), in seconds, for one or more members of a set. Redis removes each member from the set automatically when its TTL expires.

Required arguments

key

The name of the key that holds the set.

seconds

The time to live, in seconds. Each specified member is removed after this many seconds.

MEMBERS nummembers member [member ...]

The set members to specify expiration for. nummembers is the number of members, followed by that many member names.

Optional arguments

The following options modify the command's behavior. They are mutually exclusive.

NX

For each specified member, apply the expiration only when the member has no expiration.

XX

For each specified member, apply the expiration only when the member has an existing expiration.

GT

For each specified member, apply the expiration only when the new expiration is greater than the current one. A member with no expiration is treated as having an infinite TTL for the purposes of GT.

LT

For each specified member, apply the expiration only when the new expiration is less than the current one. A member with no expiration is treated as having an infinite TTL for the purposes of LT.

Examples

redis> SEXPIRE no-key 20 NX MEMBERS 2 member1 member2
1) (integer) -2
2) (integer) -2
redis> SADD myset member1 member2
(integer) 2
redis> SEXPIRE myset 10 MEMBERS 3 member1 member2 member3
1) (integer) 1
2) (integer) 1
3) (integer) -2
redis> SEXPIRE myset 20 NX MEMBERS 1 member1
1) (integer) 0
redis> SEXPIRE myset 20 GT MEMBERS 1 member1
1) (integer) 1
redis> STTL myset MEMBERS 2 member1 member2
1) (integer) 20
2) (integer) 10

Details

Only commands that remove a member clear its expiration, such as SREM. SADD leaves the TTL of an existing member untouched. You can also clear the TTL with SPERSIST, which turns the member back into a persistent member.

Calling SEXPIRE or SPEXPIRE with a zero TTL, or SEXPIREAT or SPEXPIREAT with a time in the past, removes the member from the set. If that leaves the set empty, Redis deletes the key.

Refreshing expires

You can call SEXPIRE on a member that already has a TTL. In this case, the TTL is updated to the new value.

Expired members and other set commands

Other set commands treat a member whose TTL has expired as missing, even before Redis reclaims it. SCARD is the exception: it can count expired members that haven't been removed yet.

Redis Software and Redis Cloud compatibility

Redis
Software
Redis
Cloud
Notes
❌ Standard
❌ Active-Active
❌ Standard
❌ Active-Active

Return information

One of the following:

  • Array reply. For each member:
    • Integer reply: -2 if no such member exists in the set, or the key does not exist.
    • Integer reply: 0 if the specified NX, XX, GT, or LT condition was not met.
    • Integer reply: 1 if the expiration time was set or updated.
    • Integer reply: 2 if the member was removed because the TTL is 0.
  • Simple error reply:
    • if parsing failed, mandatory arguments are missing, unknown arguments are specified, or argument values are of the wrong type or out of range.
    • if the key exists but is not a set.

See also

SPEXPIRE | SEXPIREAT | SPEXPIREAT | SADDEX | STTL | SPTTL | SEXPIRETIME | SPEXPIRETIME | SPERSIST

RATE THIS PAGE
Back to top ↑