Delete many keys in redis

hello everyone, I want to delete many keys from redis and for now I have created a list of keys and I am doing -

    |> Enum.each(RedisClient.delete())

is there a better way?
Sorry if there is already a topic for this, I couldn’t find any so please reference it to me if there is.

The Redis DEL command accepts multiple, space-separated keys for deletion (docs) like DEL key1 key2 key3, so there should be a way to do the same from a client library. I’m not sure which library you’re using, though? The thread is tagged with “redix” but that doesn’t include a RedisClient module.

From what you’ve written above, I might try something like:

Enum.join(list_of_all_keys, " ")
|> RedisClient.delete()

For the redix library, it would look something like,

Redix.command!(conn, ["DEL", Enum.join(list_of_all_keys, " ")])
1 Like

agreed that many keys can be passed with del in redis but

Enum.join(list_of_all_keys, " ")
|> RedisClient.delete()

does not work, for now I created a pipeline with a lots of del commands and it works

Can you share the code implementing RedisClient.delete, or a link to the library this module comes from? It’s hard to guess whether your delete method might already be able to accept an Enumerable like list_of_all_keys directly, or why it isn’t safely passing a space-separated list through to Redis.

If you only have a few keys to delete then it might not make much of a difference, but with many keys it becomes a big deal.


FWIW, Redix uses the “RESP” format to communicate with Redis so joining arguments with spaces won’t do what you’d expect compared to the telnet interface:

Redix.command(@some_redis, ["DEL", "123 456"]) → asks to delete one key, 123 456

Redix.command(@some_redis, ["DEL", "123", "456"]) → asks to delete two keys, 123 and 456

def delete(key),
        do: exec(:del, &Redix.command(&1,["DEL", key]))

Make that look look this:

def delete(keys) when is_list(keys), do: exec(:del, &Redix.command(&1,["DEL" | key]))
def delete(key), do: exec(:del, &Redix.command(&1,["DEL", key]))

And when you call the RedisClient.delete/1 pass it either a single key or a list of keys, if al2o3cr is right in what they say, then that will work.


yeah it works, thanks.