Home > database >  Redis StackExchange delete key
Redis StackExchange delete key

Time:12-08

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);
}

CodePudding user response:

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.

  • Related