Ir al contenido principal

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, so creating a Redis DEL command for every key matching our pattern.
  • Finally the last command, redis-cli --pipe, executes every command received by the standard input, that is, all the DEL commands created in the previous step.
Note that for simplifying the example redis-cli is using the default hostname (argument -h, default value 127.0.0.1) and not setting the password by command line (argument -a, by default taken from the env variable REDISCLI_AUTH). If your conditions are not those described, you should set those arguments in the command line.

Also note that, as described in the KEYS documentation, it is "a command that should only be used in production environments with extreme care. It may ruin performance when it is executed against large databases".

Comentarios

Entradas populares de este blog

Linting C# in Visual Studio Code

Though very usual in programming environments as Javascript/Typescript, linting , or analyzing code for enforcing a set of coding style rules, is not usually present in the .NET based environments. Rule enforcing is really useful when working on team shared codebases in order to keep them coherent, what in last term reduces both development times and coding errors. A linting example Maybe a practical example would be helpful for explaining what  linting  is to the newcomers (feel free to go on if you aren't). Let's imagine you are a new member in a C# development team that has well established set of coding style rules. Instead (or apart) of putting them in a document, they've adopted a tool that checks these rules during the code building process. Your first code is such ambitious as this: namespace HelloWorld {      using System;      public class Program      {           public static void Main(string[] args)           {                Console

ESlint: Ignore unused underscore variables

Some naming conventions promote the use of the underscore character (" _ ") for those variables that must be declared but are not being used. One common case is that in which a function signature contains some variables that will not be used, as for instance the Express error handlers: app.use(function(err, req, res, next) { console.error(err.stack); res.status(500).send('Something broke!'); }); In the above example only the arguments err and res are being used, though all four must be defined in the handler signature. Thus, following the naming convention of using underscores for those unused variables, we could recode it as: app.use(function(err, _, res, __) { console.error(err.stack); res.status(500).send('Something broke!'); }); Though it makes the function more readable, it comes with a problem if using ESlint: it will blame by declaring unused variables. error '_' is defined but never used error '__' is define

Using Bitbucket app passwords with git on MacOS (OSX)

Learn how Bitbucket passwords are stored by git on MacOS.