Back to Redis

EXISTS

content/commands/exists.md

latest1.8 KB
Original Source

{{< note >}} This command's behavior varies in clustered Redis environments. See the [multi-key operations]({{< relref "/develop/using-commands/multi-key-operations" >}}) page for more information. {{< /note >}}

Returns if key exists.

The user should be aware that if the same existing key is mentioned in the arguments multiple times, it will be counted multiple times. So if somekey exists, EXISTS somekey somekey will return 2.

Examples

{{< clients-example set="cmds_generic" step="exists" description="Foundational: Check if one or more keys exist using EXISTS (returns count of existing keys, useful for conditional logic)" difficulty="beginner" >}} SET key1 "Hello" EXISTS key1 EXISTS nosuchkey SET key2 "World" EXISTS key1 key2 nosuchkey {{< /clients-example >}}

Give these commands a try in the interactive console:

{{% redis-cli %}} SET key1 "Hello" EXISTS key1 EXISTS nosuchkey SET key2 "World" EXISTS key1 key2 nosuchkey {{% /redis-cli %}}

Redis Software and Redis Cloud compatibility

| Redis Software | Redis Cloud | <span style="min-width: 9em; display: table-cell">Notes</span> | |:----------------------|:-----------------|:------| | <span title="Supported">✅ Standard</span> <span title="Supported"><nobr>✅ Active-Active</nobr></span> | <span title="Supported">✅ Standard</span> <span title="Supported"><nobr>✅ Active-Active</nobr></span> | |

Return information

{{< multitabs id="exists-return-info" tab1="RESP2" tab2="RESP3" >}}

Integer reply: the number of keys that exist from those specified as arguments.

-tab-sep-

Integer reply: the number of keys that exist from those specified as arguments.

{{< /multitabs >}}