# SPEXPIRE

```json metadata
{
  "schema_version": 2,
  "title": "SPEXPIRE",
  "description": "Set expiry for set member using relative time to expire (milliseconds)",
  "categories": ["docs","develop","stack","oss","rs","rc","oss","kubernetes","clients"],
  "arguments": [{"display_text":"key","key_spec_index":0,"name":"key","type":"key"},{"display_text":"milliseconds","name":"milliseconds","type":"integer"},{"arguments":[{"display_text":"nx","name":"nx","token":"NX","type":"pure-token"},{"display_text":"xx","name":"xx","token":"XX","type":"pure-token"},{"display_text":"gt","name":"gt","token":"GT","type":"pure-token"},{"display_text":"lt","name":"lt","token":"LT","type":"pure-token"}],"name":"condition","optional":true,"type":"oneof"},{"arguments":[{"display_text":"nummembers","name":"nummembers","type":"integer"},{"display_text":"member","multiple":true,"name":"member","type":"string"}],"name":"members","token":"MEMBERS","type":"block"}],
  "syntax_fmt": "SPEXPIRE key milliseconds [NX | XX | GT | LT] MEMBERS nummembers\n  member [member ...]",
  "complexity": "O(N) where N is the number of specified members",
  "group": "set",
  "command_flags": ["write","fast"],
  "acl_categories": ["@write","@set","@fast"],
  "since": "8.12.0",
  "arity": -6,
  "key_specs": [{"RW":true,"begin_search":{"spec":{"index":1},"type":"index"},"find_keys":{"spec":{"keystep":1,"lastkey":0,"limit":0},"type":"range"},"update":true}],
  "tableOfContents": {"sections":[{"id":"required-arguments","title":"Required arguments"},{"id":"optional-arguments","title":"Optional arguments"},{"id":"examples","title":"Examples"},{"id":"details","title":"Details"},{"id":"redis-software-and-redis-cloud-compatibility","title":"Redis Software and Redis Cloud compatibility"},{"id":"return-information","title":"Return information"},{"id":"see-also","title":"See also"},{"id":"related-topics","title":"Related topics"}]}

,
  "codeExamples": []
}
```

This command works like [`SEXPIRE`](https://redis.io/docs/latest/commands/sexpire), but the expiration of a member is specified in milliseconds instead of seconds.

## Required arguments

<details open><summary><code>key</code></summary>

The name of the key that holds the set.

</details>

<details open><summary><code>milliseconds</code></summary>

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

</details>

<details open><summary><code>MEMBERS nummembers member [member ...]</code></summary>

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

</details>

## Optional arguments

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

<details open><summary><code>NX</code></summary>

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

</details>

<details open><summary><code>XX</code></summary>

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

</details>

<details open><summary><code>GT</code></summary>

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`.

</details>

<details open><summary><code>LT</code></summary>

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`.

</details>

## Examples

```redis
redis> SADD myset member1 member2
(integer) 2
redis> SPEXPIRE myset 2000 MEMBERS 2 member1 member2
1) (integer) 1
2) (integer) 1
redis> SPEXPIRE myset 500 LT MEMBERS 1 member1
1) (integer) 1
redis> SPTTL myset MEMBERS 2 member1 member2
1) (integer) 491
2) (integer) 1982
```

## Details

See [`SEXPIRE`](https://redis.io/docs/latest/commands/sexpire#details) for how member expiration interacts with other set commands. Calling `SPEXPIRE` with a TTL of 0 removes the member.

## Redis Software and Redis Cloud compatibility

| Redis<br />Software | Redis<br />Cloud | <span style="min-width: 9em; display: table-cell">Notes</span> |
|:----------------------|:-----------------|:------|
| <span title="Not supported">&#x274c; Standard</span><br /><span title="Not supported"><nobr>&#x274c; Active-Active</nobr></span> | <span title="Not supported">&#x274c; Standard</span><br /><span title="Not supported"><nobr>&#x274c; Active-Active</nobr></span> |  |

## Return information

**RESP2:**

One of the following:
* [Array reply](https://redis.io/docs/latest/develop/reference/protocol-spec#arrays). For each member:
    - [Integer reply](https://redis.io/docs/latest/develop/reference/protocol-spec#integers): `-2` if no such member exists in the set, or the key does not exist.
    - [Integer reply](https://redis.io/docs/latest/develop/reference/protocol-spec#integers): `0` if the specified `NX`, `XX`, `GT`, or `LT` condition was not met.
    - [Integer reply](https://redis.io/docs/latest/develop/reference/protocol-spec#integers): `1` if the expiration time was set or updated.
    - [Integer reply](https://redis.io/docs/latest/develop/reference/protocol-spec#integers): `2` if the member was removed because the TTL is 0.
* [Simple error reply](https://redis.io/docs/latest/develop/reference/protocol-spec#simple-errors):
    - 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.

**RESP3:**

One of the following:
* [Array reply](https://redis.io/docs/latest/develop/reference/protocol-spec#arrays). For each member:
    - [Integer reply](https://redis.io/docs/latest/develop/reference/protocol-spec#integers): `-2` if no such member exists in the set, or the key does not exist.
    - [Integer reply](https://redis.io/docs/latest/develop/reference/protocol-spec#integers): `0` if the specified `NX`, `XX`, `GT`, or `LT` condition was not met.
    - [Integer reply](https://redis.io/docs/latest/develop/reference/protocol-spec#integers): `1` if the expiration time was set or updated.
    - [Integer reply](https://redis.io/docs/latest/develop/reference/protocol-spec#integers): `2` if the member was removed because the TTL is 0.
* [Simple error reply](https://redis.io/docs/latest/develop/reference/protocol-spec#simple-errors):
    - 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

[`SEXPIRE`](https://redis.io/docs/latest/commands/sexpire) | [`SEXPIREAT`](https://redis.io/docs/latest/commands/sexpireat) | [`SPEXPIREAT`](https://redis.io/docs/latest/commands/spexpireat) | [`SADDEX`](https://redis.io/docs/latest/commands/saddex) | [`STTL`](https://redis.io/docs/latest/commands/sttl) | [`SPTTL`](https://redis.io/docs/latest/commands/spttl) | [`SEXPIRETIME`](https://redis.io/docs/latest/commands/sexpiretime) | [`SPEXPIRETIME`](https://redis.io/docs/latest/commands/spexpiretime) | [`SPERSIST`](https://redis.io/docs/latest/commands/spersist)

## Related topics

- [Set member expiration](https://redis.io/docs/latest/develop/data-types/sets#member-expiration)

