Key commands allow you to manage keys across all data types, handle expiration, and perform key-level operations.
DEL
Delete one or more keys.
Syntax
One or more keys to delete
Return value
The number of keys that were removed
Examples
redis-cli> SET key1 "Hello"
OK
redis-cli> SET key2 "World"
OK
redis-cli> DEL key1 key2 key3
(integer) 2
Time complexity: O(N) where N is the number of keys to remove
EXISTS
Check if one or more keys exist.
Syntax
One or more keys to check
Return value
The number of keys that exist
Examples
redis-cli> SET key1 "Hello"
OK
redis-cli> EXISTS key1
(integer) 1
redis-cli> EXISTS key1 key2
(integer) 1
redis-cli> EXISTS nosuchkey
(integer) 0
Time complexity: O(N) where N is the number of keys to check
EXPIRE
Set a key’s time to live in seconds.
Syntax
The key to set expiration on
The time to live in seconds
Return value
1 if the timeout was set, 0 if the key does not exist
Examples
redis-cli> SET mykey "Hello"
OK
redis-cli> EXPIRE mykey 10
(integer) 1
redis-cli> TTL mykey
(integer) 10
Time complexity: O(1)
PEXPIRE
Set a key’s time to live in milliseconds.
Syntax
The key to set expiration on
The time to live in milliseconds
Return value
1 if the timeout was set, 0 if the key does not exist
Examples
redis-cli> SET mykey "Hello"
OK
redis-cli> PEXPIRE mykey 1500
(integer) 1
redis-cli> PTTL mykey
(integer) 1499
Time complexity: O(1)
EXPIREAT
Set the expiration for a key as a Unix timestamp (seconds).
Syntax
The key to set expiration on
Unix timestamp in seconds
Return value
1 if the timeout was set, 0 if the key does not exist
Examples
redis-cli> SET mykey "Hello"
OK
redis-cli> EXPIREAT mykey 1735689600
(integer) 1
Time complexity: O(1)
PEXPIREAT
Set the expiration for a key as a Unix timestamp (milliseconds).
Syntax
PEXPIREAT key timestamp_ms
The key to set expiration on
Unix timestamp in milliseconds
Return value
1 if the timeout was set, 0 if the key does not exist
Examples
redis-cli> SET mykey "Hello"
OK
redis-cli> PEXPIREAT mykey 1735689600000
(integer) 1
Time complexity: O(1)
PERSIST
Remove the expiration from a key.
Syntax
Return value
1 if the timeout was removed, 0 if the key does not exist or does not have an expiration
Examples
redis-cli> SET mykey "Hello"
OK
redis-cli> EXPIRE mykey 10
(integer) 1
redis-cli> TTL mykey
(integer) 10
redis-cli> PERSIST mykey
(integer) 1
redis-cli> TTL mykey
(integer) -1
Time complexity: O(1)
TTL
Get the time to live for a key in seconds.
Syntax
Return value
TTL in seconds, -1 if the key exists but has no expiration, -2 if the key does not exist
Examples
redis-cli> SET mykey "Hello"
OK
redis-cli> EXPIRE mykey 10
(integer) 1
redis-cli> TTL mykey
(integer) 10
Time complexity: O(1)
PTTL
Get the time to live for a key in milliseconds.
Syntax
Return value
TTL in milliseconds, -1 if the key exists but has no expiration, -2 if the key does not exist
Examples
redis-cli> SET mykey "Hello"
OK
redis-cli> PEXPIRE mykey 1500
(integer) 1
redis-cli> PTTL mykey
(integer) 1499
Time complexity: O(1)
TYPE
Determine the type stored at a key.
Syntax
Return value
Type of the key: string, list, set, hash, zset, or none if the key does not exist
Examples
redis-cli> SET key1 "value"
OK
redis-cli> LPUSH key2 "a"
(integer) 1
redis-cli> SADD key3 "a"
(integer) 1
redis-cli> TYPE key1
string
redis-cli> TYPE key2
list
redis-cli> TYPE key3
set
Time complexity: O(1)
KEYS
Find all keys matching a glob pattern.
Syntax
Glob-style pattern (* matches any, ? matches one character, […] matches character set)
Return value
List of keys matching the pattern
Examples
redis-cli> MSET firstname Jack lastname Stuntman age 35
OK
redis-cli> KEYS *name*
1) "firstname"
2) "lastname"
redis-cli> KEYS a??
1) "age"
redis-cli> KEYS *
1) "firstname"
2) "lastname"
3) "age"
Time complexity: O(N) where N is the number of keys in the database
Warning: Use KEYS only in development. For production, use SCAN.
SCAN
Incrementally iterate over keys.
Syntax
SCAN cursor [MATCH pattern] [COUNT count]
The cursor position (use 0 to start a new iteration)
Glob pattern to filter keys
Hint for how many keys to return per iteration (default: 10)
Return value
Array with two elements: [0] next cursor (0 when iteration is complete), [1] array of keys
Examples
redis-cli> SCAN 0
1) "17"
2) 1) "key:12"
2) "key:8"
3) "key:4"
redis-cli> SCAN 17
1) "0"
2) 1) "key:14"
2) "key:16"
redis-cli> SCAN 0 MATCH key:1* COUNT 100
1) "0"
2) 1) "key:12"
2) "key:14"
3) "key:16"
Time complexity: O(1) for every call, O(N) for a complete iteration
DBSIZE
Return the number of keys in the current database.
Syntax
Return value
Examples
redis-cli> MSET key1 "a" key2 "b" key3 "c"
OK
redis-cli> DBSIZE
(integer) 3
Time complexity: O(1)
FLUSHDB
Delete all keys in the current database.
Syntax
Return value
Examples
redis-cli> SET key1 "Hello"
OK
redis-cli> SET key2 "World"
OK
redis-cli> FLUSHDB
OK
redis-cli> DBSIZE
(integer) 0
Time complexity: O(N) where N is the number of keys in the database
RENAME
Rename a key.
Syntax
Return value
OK on success, error if the source key does not exist
Examples
redis-cli> SET mykey "Hello"
OK
redis-cli> RENAME mykey mynewkey
OK
redis-cli> GET mynewkey
"Hello"
Time complexity: O(1)
RENAMENX
Rename a key only if the new key does not exist.
Syntax
Return value
1 if the key was renamed, 0 if the new key already exists
Examples
redis-cli> SET mykey "Hello"
OK
redis-cli> SET myotherkey "World"
OK
redis-cli> RENAMENX mykey myotherkey
(integer) 0
redis-cli> RENAMENX mykey mynewkey
(integer) 1
Time complexity: O(1)
UNLINK
Delete one or more keys asynchronously.
Syntax
One or more keys to delete
Return value
The number of keys that were unlinked
Examples
redis-cli> SET key1 "Hello"
OK
redis-cli> SET key2 "World"
OK
redis-cli> UNLINK key1 key2 key3
(integer) 2
Time complexity: O(1) for each key, O(N) overall where N is the number of keys
Note: UNLINK is similar to DEL but performs the actual memory reclamation asynchronously.
COPY
Copy a key to another key.
Syntax
COPY source destination [REPLACE]
Overwrite the destination key if it exists
Return value
1 if the key was copied, 0 otherwise
Examples
redis-cli> SET mykey "Hello"
OK
redis-cli> COPY mykey mynewkey
(integer) 1
redis-cli> GET mynewkey
"Hello"
Time complexity: O(N) where N is the size of the value
RANDOMKEY
Return a random key from the keyspace.
Syntax
Return value
A random key, or null if the database is empty
Examples
redis-cli> MSET key1 "a" key2 "b" key3 "c"
OK
redis-cli> RANDOMKEY
"key2"
redis-cli> RANDOMKEY
"key1"
Time complexity: O(1)
TOUCH
Alter the last access time of one or more keys.
Syntax
One or more keys to touch
Return value
The number of keys that were touched
Examples
redis-cli> SET key1 "Hello"
OK
redis-cli> SET key2 "World"
OK
redis-cli> TOUCH key1 key2
(integer) 2
Time complexity: O(N) where N is the number of keys
Redis Compatibility
Kora implements all standard Redis key management commands with identical semantics.