4

I am trying to delete a Redis Key. I am using the StackExchange.Redis library and have tried searching on StackOverflow for a way to delete a key. I found this link: StackExchange Redis delete all keys that start with

But my library does not have a method called Database.KeyDelete. How do I get that method?

public void DeleteCacheByKey(string Key)
{
    ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("127.0.0..1:6379");
    var server = redis.GetServer("127.0.0..1:6379");
    redis.Database.KeyDelete(key);
}
JianYA
  • 2,750
  • 8
  • 60
  • 136

1 Answers1

3

Assuming you are using the default Redis DB, you should try like this :

public void DeleteCacheByKey(string Key)
{
    ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("127.0.0.1:6379");
    redis.GetDatabase().KeyDelete(key);
}

Note that the ConnectionMultiplexer is IDisposable. It should be diposed.

jbl
  • 15,179
  • 3
  • 34
  • 101