{
  "schema_version": 2,
  "id": "commands/getset",
  "title": "GETSET",
  "url": "https://redis.io/docs/latest/commands/getset/",
  "summary": "Returns the previous string value of a key after setting it to a new value.",
  "since": "1.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": "b9f8248e68aaa637ad68f9e41fd369bd9d2254bbb2e9e00c0617c8f20b527e28",
  "sections": [
    {
      "id": "overview",
      "title": "Overview",
      "role": "overview",
      "text": "Atomically sets `key` to `value` and returns the old value stored at `key`.\nReturns an error when `key` exists but does not hold a string value.  Any \nprevious time to live associated with the key is discarded on successful \n[`SET`](https://redis.io/docs/latest/commands/set) operation."
    },
    {
      "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>value</code></summary>\n\nThe new value to set.\n\n</details>"
    },
    {
      "id": "examples",
      "title": "Examples",
      "role": "example",
      "text": "redis> SET mykey \"Hello\"\nOK\nredis> GETSET mykey \"World\"\n\"Hello\"\nredis> GET mykey\n\"World\""
    },
    {
      "id": "details",
      "title": "Details",
      "role": "content",
      "text": ""
    },
    {
      "id": "design-pattern",
      "title": "Design pattern",
      "role": "content",
      "text": "You can use GETSET with [INCR](https://redis.io/docs/latest/commands/incr) to count events and reset the counter atomically.\nFor example, you can call [INCR](https://redis.io/docs/latest/commands/incr) on the key `mycounter` each time an event occurs. When you need to read the counter value and reset it to zero atomically, call GETSET.\nThis can be done using `GETSET mycounter \"0\"`:\n\n\nredis> INCR mycounter\n(integer) 1\nredis> GETSET mycounter \"0\"\n\"1\"\nredis> GET mycounter\n\"0\""
    },
    {
      "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> | Deprecated as of Redis v6.2.0. |"
    },
    {
      "id": "return-information",
      "title": "Return information",
      "role": "returns",
      "text": "**RESP2:**\n\nOne of the following:\n* [Bulk string reply](../../develop/reference/protocol-spec#bulk-strings): the old value stored at the key.\n* [Nil reply](../../develop/reference/protocol-spec#bulk-strings): if the key does not exist.\n\n**RESP3:**\n\nOne of the following:\n* [Bulk string reply](../../develop/reference/protocol-spec#bulk-strings): the old value stored at the key.\n* [Null reply](../../develop/reference/protocol-spec#nulls): if the key does not exist."
    }
  ],
  "examples": []
}
