DEL
Deletes one or more keys.Syntax
The key(s) to delete. Multiple keys can be specified.
The number of keys that were removed.
Examples
EXISTS
Determines whether one or more keys exist.Syntax
The key(s) to check. Multiple keys can be specified.
Number of keys that exist. If the same key is mentioned multiple times, it will be counted multiple times.
- 3.0.3: Accepts multiple
keyarguments.
Examples
EXPIRE
Sets the expiration time of a key in seconds.Syntax
The key to set expiration on.
The expiration time in seconds.
Set expiry only when the key has no expiry.
Set expiry only when the key has an existing expiry.
Set expiry only when the new expiry is greater than current one.
Set expiry only when the new expiry is less than current one.
1if the timeout was set0if the timeout was not set (key doesn’t exist or condition not met)
- 7.0.0: Added options:
NX,XX,GTandLT.
Examples
TTL
Returns the remaining time to live of a key in seconds.Syntax
The key to get TTL for.
- TTL in seconds
-1if the key exists but has no associated expire-2if the key does not exist
- 2.8.0: Added the -2 reply.
Examples
KEYS
Returns all key names that match a pattern.Syntax
The glob-style pattern to match. Supported patterns:
*matches any characters?matches a single character[abc]matches one character from the set[a-z]matches one character from the range\escapes special characters
List of keys matching the pattern.
Examples
SCAN
Iterates over the key names in the database using a cursor.Syntax
The cursor position. Use 0 to start iteration.
Only return keys matching the given pattern.
Hint for how many elements to return per call (default 10).
Only return keys of this type (string, list, set, zset, hash, stream).
Array with two elements:
- The next cursor position (0 when iteration is complete)
- Array of keys
- 6.0.0: Added the
TYPEsubcommand.
Examples
TYPE
Determines the type of value stored at a key.Syntax
The key to inspect.
Type of the key:
stringlistsetzset(sorted set)hashstreamnone(key doesn’t exist)
Examples
Related Commands
- PERSIST: Remove expiration from a key
- EXPIREAT: Set expiration as Unix timestamp
- PEXPIRE: Set expiration in milliseconds
- PTTL: Get TTL in milliseconds
- RENAME: Rename a key
- RENAMENX: Rename a key only if new key doesn’t exist
- DUMP: Serialize key value
- RESTORE: Deserialize key value
- TOUCH: Update last access time
- UNLINK: Async delete
Best Practices
Key Naming
- Use consistent naming conventions (e.g.,
object:id:field) - Keep names readable but concise
- Use colons to separate logical parts
- Avoid special characters
Expiration
- Always set TTL on temporary data
- Use EXPIRE with conditions (NX, XX, GT, LT) for precise control
- Monitor expired keys with INFO stats
Iteration
- Always use SCAN instead of KEYS in production
- Use MATCH patterns to filter results
- Adjust COUNT for optimal performance
- Handle full iterations by checking cursor = 0