{
  "schema_version": 2,
  "id": "commands/exists",
  "title": "EXISTS",
  "url": "https://redis.io/docs/latest/commands/exists/",
  "summary": "Determines whether one or more keys exist.",
  "since": "1.0.0",
  "tags": [
    "docs",
    "develop",
    "stack",
    "oss",
    "rs",
    "rc",
    "oss",
    "kubernetes",
    "clients"
  ],
  "last_updated": "2026-09-18T09:48:32+01:00",
  "page_type": "content",
  "content_hash": "1601af7e80adfd115aeccb4cd20dc67ed6dd67f2b58b65151f7a14ec40135478",
  "sections": [
    {
      "id": "required-arguments",
      "title": "Required arguments",
      "role": "content",
      "text": "<details open><summary><code>key [key ...]</code></summary>\n\nOne or more keys to check for existence. A repeated key is counted once per occurrence.\n\n</details>"
    },
    {
      "id": "examples",
      "title": "Examples",
      "role": "example",
      "text": "Foundational: Check if one or more keys exist using EXISTS (returns count of existing keys, useful for conditional logic)\n\n**Difficulty:** Beginner\n\n**Commands:** SET, EXISTS\n\n**Complexity:**\n- SET: O(1)\n- EXISTS: O(N)\n\n**Available in:** Redis CLI, C, C#, Go, Java (Asynchronous - Lettuce), Java (Reactive - Lettuce), Java (Synchronous - Jedis), JavaScript (Node.js), JavaScript (Node.js), PHP, Python, Rust (Asynchronous), Rust (Synchronous)\n\n##### Redis CLI\n\n[code example]\n\n##### C\n\n[code example]\n\n##### C#\n\n[code example]\n\n##### Go\n\n[code example]\n\n##### Java (Asynchronous - Lettuce)\n\n[code example]\n\n##### Java (Reactive - Lettuce)\n\n[code example]\n\n##### Java (Synchronous - Jedis)\n\n[code example]\n\n##### JavaScript (Node.js)\n\n[code example]\n\n##### JavaScript (Node.js)\n\n[code example]\n\n##### JavaScript (Node.js)\n\n[code example]\n\n##### JavaScript (Node.js)\n\n[code example]\n\n##### PHP\n\n[code example]\n\n##### Python\n\n[code example]\n\n##### Rust (Asynchronous)\n\n[code example]\n\n##### Rust (Synchronous)\n\n[code example]"
    },
    {
      "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[Integer reply](../../develop/reference/protocol-spec#integers): the number of keys that exist from those specified as arguments.\n\n**RESP3:**\n\n[Integer reply](../../develop/reference/protocol-spec#integers): the number of keys that exist from those specified as arguments."
    }
  ],
  "examples": [
    {
      "id": "examples-ex0",
      "language": "plaintext",
      "code": "> SET key1 \"Hello\"\n> EXISTS key1\n> EXISTS nosuchkey\n> SET key2 \"World\"\n> EXISTS key1 key2 nosuchkey",
      "section_id": "examples"
    },
    {
      "id": "examples-ex1",
      "language": "c",
      "code": "reply = redisCommand(c, \"SET key1 Hello\");\n    printf(\"%s\\n\", reply->str);\n    // >>> OK\n    freeReplyObject(reply);\n\n    reply = redisCommand(c, \"EXISTS key1\");\n    printf(\"%lld\\n\", reply->integer);\n    // >>> 1\n    freeReplyObject(reply);\n\n    reply = redisCommand(c, \"EXISTS nosuchkey\");\n    printf(\"%lld\\n\", reply->integer);\n    // >>> 0\n    freeReplyObject(reply);\n\n    reply = redisCommand(c, \"SET key2 World\");\n    printf(\"%s\\n\", reply->str);\n    // >>> OK\n    freeReplyObject(reply);\n\n    reply = redisCommand(c, \"EXISTS key1 key2 nosuchkey\");\n    printf(\"%lld\\n\", reply->integer);\n    // >>> 2\n    freeReplyObject(reply);",
      "section_id": "examples"
    },
    {
      "id": "examples-ex2",
      "language": "csharp",
      "code": "bool existsResult1 = db.StringSet(\"key1\", \"Hello\");\n        Console.WriteLine(existsResult1);  // >>> true\n\n        bool existsResult2 = db.KeyExists(\"key1\");\n        Console.WriteLine(existsResult2);  // >>> true\n\n        bool existsResult3 = db.KeyExists(\"nosuchkey\");\n        Console.WriteLine(existsResult3);  // >>> false\n\n        bool existsResult4 = db.StringSet(\"key2\", \"World\");\n        Console.WriteLine(existsResult4);  // >>> true\n\n        long existsResult5 = db.KeyExists([\"key1\", \"key2\", \"nosuchkey\"]);\n        Console.WriteLine(existsResult5);  // >>> 2",
      "section_id": "examples"
    },
    {
      "id": "examples-ex3",
      "language": "go",
      "code": "existsResult1, err := rdb.Set(ctx, \"key1\", \"Hello\", 0).Result()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(existsResult1) // >>> OK\n\n\texistsResult2, err := rdb.Exists(ctx, \"key1\").Result()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(existsResult2) // >>> 1\n\n\texistsResult3, err := rdb.Exists(ctx, \"nosuchkey\").Result()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(existsResult3) // >>> 0\n\n\texistsResult4, err := rdb.Set(ctx, \"key2\", \"World\", 0).Result()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(existsResult4) // >>> OK\n\n\texistsResult5, err := rdb.Exists(ctx, \"key1\", \"key2\", \"nosuchkey\").Result()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(existsResult5) // >>> 2",
      "section_id": "examples"
    },
    {
      "id": "examples-ex4",
      "language": "java",
      "code": "CompletableFuture<Void> existsExample = asyncCommands.set(\"key1\", \"Hello\").thenCompose(res1 -> {\n                System.out.println(res1); // >>> OK\n\n                return asyncCommands.exists(\"key1\");\n            }).thenCompose(res2 -> {\n                System.out.println(res2); // >>> 1\n\n                return asyncCommands.exists(\"nosuchkey\");\n            }).thenCompose(res3 -> {\n                System.out.println(res3); // >>> 0\n\n                return asyncCommands.set(\"key2\", \"World\");\n            }).thenCompose(res4 -> {\n                System.out.println(res4); // >>> OK\n\n                return asyncCommands.exists(\"key1\", \"key2\", \"nosuchkey\");\n            }).thenAccept(res5 -> {\n                System.out.println(res5); // >>> 2\n            }).toCompletableFuture();",
      "section_id": "examples"
    },
    {
      "id": "examples-ex5",
      "language": "java",
      "code": "Mono<Void> existsExample = reactiveCommands.set(\"key1\", \"Hello\").doOnNext(res1 -> {\n                System.out.println(res1); // >>> OK\n            }).then(reactiveCommands.exists(\"key1\")).doOnNext(res2 -> {\n                System.out.println(res2); // >>> 1\n            }).then(reactiveCommands.exists(\"nosuchkey\")).doOnNext(res3 -> {\n                System.out.println(res3); // >>> 0\n            }).then(reactiveCommands.set(\"key2\", \"World\")).doOnNext(res4 -> {\n                System.out.println(res4); // >>> OK\n            }).then(reactiveCommands.exists(\"key1\", \"key2\", \"nosuchkey\")).doOnNext(res5 -> {\n                System.out.println(res5); // >>> 2\n            }).then();",
      "section_id": "examples"
    },
    {
      "id": "examples-ex6",
      "language": "java",
      "code": "String existsResult1 = jedis.set(\"key1\", \"Hello\");\n        System.out.println(existsResult1); // >>> OK\n\n        boolean existsResult2 = jedis.exists(\"key1\");\n        System.out.println(existsResult2); // >>> true\n\n        boolean existsResult3 = jedis.exists(\"nosuchkey\");\n        System.out.println(existsResult3); // >>> false\n\n        String existsResult4 = jedis.set(\"key2\", \"World\");\n        System.out.println(existsResult4); // >>> OK\n\n        long existsResult5 = jedis.exists(\"key1\", \"key2\", \"nosuchkey\");\n        System.out.println(existsResult5); // >>> 2",
      "section_id": "examples"
    },
    {
      "id": "examples-ex7",
      "language": "javascript",
      "code": "const existsRes1 = await client.set('key1', 'Hello');\nconsole.log(existsRes1); // OK\n\nconst existsRes2 = await client.exists('key1');\nconsole.log(existsRes2); // 1\n\nconst existsRes3 = await client.exists('nosuchkey');\nconsole.log(existsRes3); // 0\n\nconst existsRes4 = await client.set('key2', 'World');\nconsole.log(existsRes4); // OK\n\nconst existsRes5 = await client.exists(['key1', 'key2', 'nosuchkey']);\nconsole.log(existsRes5); // 2",
      "section_id": "examples"
    },
    {
      "id": "examples-ex8",
      "language": "javascript",
      "code": "console.log(await redis.set('key1', 'Hello')); // >>> OK\n\nconsole.log(await redis.exists('key1')); // >>> 1\nconsole.log(await redis.exists('nosuchkey')); // >>> 0\n\nconsole.log(await redis.set('key2', 'World')); // >>> OK\n\nconst existsResult = await redis.exists('key1', 'key2', 'nosuchkey');\nconsole.log(existsResult); // >>> 2",
      "section_id": "examples"
    },
    {
      "id": "examples-ex9",
      "language": "javascript",
      "code": "const existsRes1 = await client.set('key1', 'Hello');\nconsole.log(existsRes1); // OK\n\nconst existsRes2 = await client.exists('key1');\nconsole.log(existsRes2); // 1\n\nconst existsRes3 = await client.exists('nosuchkey');\nconsole.log(existsRes3); // 0\n\nconst existsRes4 = await client.set('key2', 'World');\nconsole.log(existsRes4); // OK\n\nconst existsRes5 = await client.exists(['key1', 'key2', 'nosuchkey']);\nconsole.log(existsRes5); // 2",
      "section_id": "examples"
    },
    {
      "id": "examples-ex10",
      "language": "javascript",
      "code": "console.log(await redis.set('key1', 'Hello')); // >>> OK\n\nconsole.log(await redis.exists('key1')); // >>> 1\nconsole.log(await redis.exists('nosuchkey')); // >>> 0\n\nconsole.log(await redis.set('key2', 'World')); // >>> OK\n\nconst existsResult = await redis.exists('key1', 'key2', 'nosuchkey');\nconsole.log(existsResult); // >>> 2",
      "section_id": "examples"
    },
    {
      "id": "examples-ex11",
      "language": "php",
      "code": "$existsResult1 = $r->set('key1', 'Hello');\n        echo $existsResult1 . PHP_EOL; // >>> OK\n\n        $existsResult2 = $r->exists('key1');\n        echo $existsResult2 . PHP_EOL; // >>> 1\n\n        $existsResult3 = $r->exists('nosuchkey');\n        echo $existsResult3 . PHP_EOL; // >>> 0\n\n        $existsResult4 = $r->set('key2', 'World');\n        echo $existsResult4 . PHP_EOL; // >>> OK\n\n        $existsResult5 = $r->exists('key1', 'key2', 'nosuchkey');\n        echo $existsResult5 . PHP_EOL; // >>> 2",
      "section_id": "examples"
    },
    {
      "id": "examples-ex12",
      "language": "python",
      "code": "res = r.set(\"key1\", \"Hello\")\nprint(res)\n# >>> True\n\nres = r.exists(\"key1\")\nprint(res)\n# >>> 1\n\nres = r.exists(\"nosuchkey\")\nprint(res)\n# >>> 0\n\nres = r.set(\"key2\", \"World\")\nprint(res)\n# >>> True\n\nres = r.exists(\"key1\", \"key2\", \"nosuchkey\")\nprint(res)\n# >>> 2",
      "section_id": "examples"
    },
    {
      "id": "examples-ex13",
      "language": "rust",
      "code": "if let Ok(res) = r.set(\"key1\", \"Hello\").await {\n            let res: String = res;\n            println!(\"{res}\");    // >>> OK\n        }\n\n        match r.exists(\"key1\").await {\n            Ok(res) => {\n                let res: i32 = res;\n                println!(\"{res}\");    // >>> 1\n            },\n            Err(e) => {\n                println!(\"Error checking key existence: {e}\");\n                return;\n            }\n        }\n\n        match r.exists(\"nosuchkey\").await {\n            Ok(res) => {\n                let res: i32 = res;\n                println!(\"{res}\");    // >>> 0\n            },\n            Err(e) => {\n                println!(\"Error checking key existence: {e}\");\n                return;\n            }\n        }\n\n        if let Ok(res) = r.set(\"key2\", \"World\").await {\n            let res: String = res;\n            println!(\"{res}\");    // >>> OK\n        }\n\n        match r.exists(&[\"key1\", \"key2\", \"nosuchkey\"]).await {\n            Ok(res) => {\n                let res: i32 = res;\n                println!(\"{res}\");    // >>> 2\n            },\n            Err(e) => {\n                println!(\"Error checking key existence: {e}\");\n                return;\n            }\n        }",
      "section_id": "examples"
    },
    {
      "id": "examples-ex14",
      "language": "rust",
      "code": "if let Ok(res) = r.set(\"key1\", \"Hello\") {\n            let res: String = res;\n            println!(\"{res}\");    // >>> OK\n        }\n\n        match r.exists(\"key1\") {\n            Ok(res) => {\n                let res: i32 = res;\n                println!(\"{res}\");    // >>> 1\n            },\n            Err(e) => {\n                println!(\"Error checking key existence: {e}\");\n                return;\n            }\n        }\n\n        match r.exists(\"nosuchkey\") {\n            Ok(res) => {\n                let res: i32 = res;\n                println!(\"{res}\");    // >>> 0\n            },\n            Err(e) => {\n                println!(\"Error checking key existence: {e}\");\n                return;\n            }\n        }\n\n        if let Ok(res) = r.set(\"key2\", \"World\") {\n            let res: String = res;\n            println!(\"{res}\");    // >>> OK\n        }\n\n        match r.exists(&[\"key1\", \"key2\", \"nosuchkey\"]) {\n            Ok(res) => {\n                let res: i32 = res;\n                println!(\"{res}\");    // >>> 2\n            },\n            Err(e) => {\n                println!(\"Error checking key existence: {e}\");\n                return;\n            }\n        }",
      "section_id": "examples"
    }
  ]
}
