{
  "schema_version": 2,
  "id": "commands/sexpire",
  "title": "SEXPIRE",
  "url": "https://redis.io/docs/latest/commands/sexpire/",
  "summary": "Set expiry for set member using relative time to expire (seconds)",
  "since": "8.12.0",
  "tags": [
    "docs",
    "develop",
    "stack",
    "oss",
    "rs",
    "rc",
    "oss",
    "kubernetes",
    "clients"
  ],
  "last_updated": "2026-11-23T06:19:08-08:00",
  "page_type": "content",
  "content_hash": "629c473b373bae8dee4f55ad507f2bc2ae3d9dc0c81c7d9b178528283e1f87a2",
  "sections": [
    {
      "id": "overview",
      "title": "Overview",
      "role": "overview",
      "text": "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."
    },
    {
      "id": "required-arguments",
      "title": "Required arguments",
      "role": "content",
      "text": "<details open><summary><code>key</code></summary>\n\nThe name of the key that holds the set.\n\n</details>\n\n<details open><summary><code>seconds</code></summary>\n\nThe time to live, in seconds. Each specified member is removed after this many seconds.\n\n</details>\n\n<details open><summary><code>MEMBERS nummembers member [member ...]</code></summary>\n\nThe set members to specify expiration for. `nummembers` is the number of members, followed by that many member names.\n\n</details>"
    },
    {
      "id": "optional-arguments",
      "title": "Optional arguments",
      "role": "parameters",
      "text": "The following options modify the command's behavior. They are mutually exclusive.\n\n<details open><summary><code>NX</code></summary>\n\nFor each specified member, apply the expiration only when the member has no expiration.\n\n</details>\n\n<details open><summary><code>XX</code></summary>\n\nFor each specified member, apply the expiration only when the member has an existing expiration.\n\n</details>\n\n<details open><summary><code>GT</code></summary>\n\nFor 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`.\n\n</details>\n\n<details open><summary><code>LT</code></summary>\n\nFor 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`.\n\n</details>"
    },
    {
      "id": "examples",
      "title": "Examples",
      "role": "example",
      "text": "[code example]"
    },
    {
      "id": "details",
      "title": "Details",
      "role": "content",
      "text": "Only commands that remove a member clear its expiration, such as [`SREM`](https://redis.io/docs/latest/commands/srem). [`SADD`](https://redis.io/docs/latest/commands/sadd) leaves the TTL of an existing member untouched. You can also clear the TTL with [`SPERSIST`](https://redis.io/docs/latest/commands/spersist), which turns the member back into a persistent member.\n\nCalling `SEXPIRE` or [`SPEXPIRE`](https://redis.io/docs/latest/commands/spexpire) with a zero TTL, or [`SEXPIREAT`](https://redis.io/docs/latest/commands/sexpireat) or [`SPEXPIREAT`](https://redis.io/docs/latest/commands/spexpireat) with a time in the past, removes the member from the set. If that leaves the set empty, Redis deletes the key."
    },
    {
      "id": "refreshing-expires",
      "title": "Refreshing expires",
      "role": "content",
      "text": "You can call `SEXPIRE` on a member that already has a TTL. In this case, the TTL is _updated_ to the new value."
    },
    {
      "id": "expired-members-and-other-set-commands",
      "title": "Expired members and other set commands",
      "role": "content",
      "text": "Other set commands treat a member whose TTL has expired as missing, even before Redis reclaims it. [`SCARD`](https://redis.io/docs/latest/commands/scard) is the exception: it can count expired members that haven't been removed yet."
    },
    {
      "id": "redis-software-and-redis-cloud-compatibility",
      "title": "Redis Software and Redis Cloud compatibility",
      "role": "content",
      "text": "| Redis<br />Software | Redis<br />Cloud | <span style=\"min-width: 9em; display: table-cell\">Notes</span> |\n|:----------------------|:-----------------|:------|\n| <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> |  |"
    },
    {
      "id": "return-information",
      "title": "Return information",
      "role": "returns",
      "text": "**RESP2:**\n\nOne of the following:\n* [Array reply](/content/develop/reference/protocol-spec#arrays). For each member:\n    - [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.\n    - [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.\n    - [Integer reply](https://redis.io/docs/latest/develop/reference/protocol-spec#integers): `1` if the expiration time was set or updated.\n    - [Integer reply](https://redis.io/docs/latest/develop/reference/protocol-spec#integers): `2` if the member was removed because the TTL is 0.\n* [Simple error reply](https://redis.io/docs/latest/develop/reference/protocol-spec#simple-errors):\n    - if parsing failed, mandatory arguments are missing, unknown arguments are specified, or argument values are of the wrong type or out of range.\n    - if the key exists but is not a set.\n\n**RESP3:**\n\nOne of the following:\n* [Array reply](https://redis.io/docs/latest/develop/reference/protocol-spec#arrays). For each member:\n    - [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.\n    - [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.\n    - [Integer reply](https://redis.io/docs/latest/develop/reference/protocol-spec#integers): `1` if the expiration time was set or updated.\n    - [Integer reply](https://redis.io/docs/latest/develop/reference/protocol-spec#integers): `2` if the member was removed because the TTL is 0.\n* [Simple error reply](https://redis.io/docs/latest/develop/reference/protocol-spec#simple-errors):\n    - if parsing failed, mandatory arguments are missing, unknown arguments are specified, or argument values are of the wrong type or out of range.\n    - if the key exists but is not a set."
    },
    {
      "id": "see-also",
      "title": "See also",
      "role": "related",
      "text": "[`SPEXPIRE`](https://redis.io/docs/latest/commands/spexpire) | [`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)"
    },
    {
      "id": "related-topics",
      "title": "Related topics",
      "role": "related",
      "text": "- [Set member expiration](https://redis.io/docs/latest/develop/data-types/sets#member-expiration)"
    }
  ],
  "examples": [
    {
      "id": "examples-ex0",
      "language": "redis",
      "code": "redis> SEXPIRE no-key 20 NX MEMBERS 2 member1 member2\n1) (integer) -2\n2) (integer) -2\nredis> SADD myset member1 member2\n(integer) 2\nredis> SEXPIRE myset 10 MEMBERS 3 member1 member2 member3\n1) (integer) 1\n2) (integer) 1\n3) (integer) -2\nredis> SEXPIRE myset 20 NX MEMBERS 1 member1\n1) (integer) 0\nredis> SEXPIRE myset 20 GT MEMBERS 1 member1\n1) (integer) 1\nredis> STTL myset MEMBERS 2 member1 member2\n1) (integer) 20\n2) (integer) 10",
      "section_id": "examples"
    }
  ]
}
