{
  "schema_version": 2,
  "id": "commands/setex",
  "title": "SETEX",
  "url": "https://redis.io/docs/latest/commands/setex/",
  "summary": "Sets the string value and expiration time of a key. Creates the key if it doesn't exist.",
  "since": "2.0.0",
  "tags": [
    "docs",
    "develop",
    "stack",
    "oss",
    "rs",
    "rc",
    "oss",
    "kubernetes",
    "clients"
  ],
  "last_updated": "2026-09-18T16:45:12+01:00",
  "page_type": "content",
  "content_hash": "95d24fa358da3bd85259c5f549172d0775911da80ac9df99b89d8a0a4d926716",
  "sections": [
    {
      "id": "overview",
      "title": "Overview",
      "role": "overview",
      "text": "Set `key` to hold the string `value` and set `key` to timeout after a given\nnumber of seconds.\nThis command is equivalent to:\n\n[code example]\n\nAn error is returned when `seconds` is invalid."
    },
    {
      "id": "required-arguments",
      "title": "Required arguments",
      "role": "content",
      "text": "<details open><summary><code>key</code></summary>\n\nThe name of the key.\n\n</details>\n\n<details open><summary><code>seconds</code></summary>\n\nThe time to live, in seconds.\n\n</details>\n\n<details open><summary><code>value</code></summary>\n\nThe value to set.\n\n</details>"
    },
    {
      "id": "examples",
      "title": "Examples",
      "role": "example",
      "text": "redis> SETEX mykey 10 \"Hello\"\nOK\nredis> TTL mykey\n(integer) 10\nredis> GET mykey\n\"Hello\""
    },
    {
      "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=\"Supported\">&#x2705; Standard</span><br /><span title=\"Supported\"><nobr>&#x2705; Active-Active</nobr></span> | <span title=\"Supported\">&#x2705; Standard</span><br /><span title=\"Supported\"><nobr>&#x2705; Active-Active</nobr></span> |  |"
    },
    {
      "id": "return-information",
      "title": "Return information",
      "role": "returns",
      "text": "**RESP2:**\n\n[Simple string reply](../../develop/reference/protocol-spec#simple-strings): `OK`.\n\n**RESP3:**\n\n[Simple string reply](../../develop/reference/protocol-spec#simple-strings): `OK`."
    },
    {
      "id": "see-also",
      "title": "See also",
      "role": "related",
      "text": "[`TTL`](https://redis.io/docs/latest/commands/ttl)"
    }
  ],
  "examples": [
    {
      "id": "overview-ex0",
      "language": "plaintext",
      "code": "SET key value EX seconds",
      "section_id": "overview"
    }
  ]
}
