1

I am using Redis Cache in my project. Now there is a situation that cache is dynamically generate like "KeyPrefix"+i i is a dynamic value

Now I want to delete all keys that having prefix "KeyPrefix"

How it posible as in my code I can delete only matching key. But I need to delete all keys that containing some prefix.

  IDatabase cache = RedisDistributedCacheManager.GetDatabase();
                cache.KeyDelete(cacheKey);

Thanks Dalvir

Dalvir Singh
  • 393
  • 3
  • 10
  • 24
  • possible duplicate of [Redis Stack Exchange how to delete or get keys by pattern](http://stackoverflow.com/questions/26488830/redis-stack-exchange-how-to-delete-or-get-keys-by-pattern) – The Real Bill Sep 30 '15 at 13:08

1 Answers1

1

Redis key scan targets a server, not a single database. So you may get the server variable and then use the Keys method to get all needed keys:

var server = conn.GetServer(someServer);    
server.Keys(pattern: "*foo*"))

Check the doc first: https://github.com/StackExchange/StackExchange.Redis/blob/master/Docs/KeysScan.md

Ilya Chumakov
  • 20,874
  • 7
  • 77
  • 111