redis-cli --scan --pattern: list keys without blocking Redis
The commands on this page have not been executed by our verification runner yet.updated 2026-10-20
- tool
- Redis
- command
- redis-cli
- flag
- --scan [--pattern P] [--count N]
- default
- off
redis-cli --scan --pattern 'user:*' prints every matching key by calling SCAN repeatedly, a few keys at a time. Unlike KEYS user:*, which walks the whole keyspace in one command and blocks every other client until it finishes, SCAN keeps the server responsive, so it is the safe way to look for keys in production.
Examples
redis-cli --scan --pattern 'session:*' # list matching keys
redis-cli --scan --pattern 'session:*' | wc -l # count them
redis-cli --scan --pattern 'cache:*' --count 1000 # bigger batches (a hint, not a limit)
redis-cli --scan --type hash # only hashes (Redis 6+)
redis-cli -n 2 --scan --pattern 'job:*' # database 2Delete keys by pattern
redis-cli --scan --pattern 'cache:*' | xargs -r -n 500 redis-cli UNLINKUNLINK frees memory in the background, so it does not block like DEL on large values.
What SCAN guarantees
| Property | SCAN | KEYS |
|---|---|---|
| Blocks the server | no, small steps | yes, for the whole keyspace |
| Returns keys present the whole time | yes, at least once | yes |
| Duplicates possible | yes, deduplicate if it matters | no |
| Keys added or removed during the scan | may or may not be returned | n/a |
| Safe on a big production instance | yes | no |
Sources
Related pages
glossary · sqlWhat is a database index?A database index is a sorted structure, usually a B-tree, that finds rows by column value without reading the whole table, at the cost of slower writes.cheat sheets · postgresqlpsql cheat sheet: connect, list, describe and run SQL filespsql cheat sheet: connection strings, \l \dt \d+ and \du, switching database, expanded output, timing, running files with -f, \copy to CSV, and catalog queries.errors · sqliteSQLite: database is locked (SQLITE_BUSY)SQLite returns database is locked (SQLITE_BUSY) when another connection holds a write lock. Fix it with a busy timeout, WAL mode, and short write transactions.