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

Last Post!

nightfury17200

nightfury17200

yeah it works, thanks.

Where Next?

Popular in Questions Top

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
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

Other popular topics Top

New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
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

We're in Beta

About us Mission Statement