nightfury17200

nightfury17200

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 -

list_of_all_keys
    |> 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.

Marked As Solved

al2o3cr

al2o3cr

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

Also Liked

adamwight

adamwight

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.

NobbZ

NobbZ

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.

adamwight

adamwight

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, " ")])

Where Next?

Popular in Questions Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Other popular topics Top

chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

We're in Beta

About us Mission Statement