Ir al contenido principal

Entradas

Mostrando las entradas etiquetadas como redis

Delete Redis keys matching a pattern

Though Redis allows searching entries based on a key pattern (command KEYS ), it doesn't allow bulk deleting entries based on a key pattern, what can be useful in many scenarios, as that in which you need to refresh part of your cache and don't want to entirely drop it. However, we can do it by using the redis-cli pipe mode, that allows redis-cli executing a list of commands received by the standard input. Let's see how to delete a set of entries based on a key pattern: redis-cli --raw KEYS mykeyprefix* | awk '{print "DEL "$0}' | redis-cli --pipe As can be seen, three commands are used: The first command, redis-cli --raw KEYS mykeyprefix* , executes the Redis command KEYS mykeyprefix* for getting all the keys starting by "mykeyprefix", and sends the raw result to the standard output. awk '{print "DEL "$0}' basically transforms the previous command output by prefixing every line with DEL , ...

Useful redis commands (in a nutshell)

redis-cli , the Redis command line tool, is a quick shortcut for running the most common operations that require our everyday job. Let's review them in a nutshell: To open a server session, run the console command: shell> redis-cli -h <hostname> The password can be specified in the above command by setting the -a <password> argument, but for security reasons it's better to authenticate once in the redis-cli console: redis> AUTH [username] password Again from the console, to check if one ore more keys exist: redis> EXISTS <key> [<key> ...] To search keys matching a given pattern: redis> KEYS <pattern> To get an entry value given its key: redis> GET <key> To delete an entry given its key: redis> DEL <key> To quit the redis-cli session: redis> EXIT Lastly, to access to the redis-cli command reference click here .