title: Redis wildcard delete
body: <p>I'm using the the stackexchange redis provider and I'm trying to delete all keys starting with a certain key.</p>
  
  <pre><code>private readonly Lazy&lt;IEnumerable&lt;IServer&gt;&gt; allServers = new Lazy&lt;IEnumerable&lt;IServer&gt;&gt;(() =&gt; Connection.GetEndPoints().Select(s =&gt; Connection.GetServer(s)));
  private const int DbId = 1;
  private const int ScanPageSize = 1 &lt;&lt; 14;
  
  public void RemoveByPattern(string pattern)
  {
      var keys = this.AllServers.Value.SelectMany(x =&gt; x.Keys(DbId, pattern + "*", ScanPageSize)).Distinct().ToArray();
      if (!keys.Any())
          return;
      this.Database.KeyDelete(keys);
  }
  </code></pre>
  
  <p>^^ This is the method I've come up which works fine most of the time. But as soon as I put a bit of load on the system I'm getting errors that look like this:</p>
  
  <pre><code>  System.TimeoutException: Timeout performing SCAN, inst: 0, mgr: Inactive, queue: 11, qu=11, qs=0, qc=0, wr=0/1, in=0/0
       at StackExchange.Redis.ConnectionMultiplexer.ExecuteSyncImpl (StackExchange.Redis.StrongName, Version=1.0.316.0, Culture=neutral, PublicKeyToken=c219ff1ca8c2ce46)
       at StackExchange.Redis.RedisServer.ExecuteSync (StackExchange.Redis.StrongName, Version=1.0.316.0, Culture=neutral, PublicKeyToken=c219ff1ca8c2ce46)
       at StackExchange.Redis.RedisBase+CursorEnumerable`1.GetNextPageSync (StackExchange.Redis.StrongName, Version=1.0.316.0, Culture=neutral, PublicKeyToken=c219ff1ca8c2ce46)
       at StackExchange.Redis.RedisBase+CursorEnumerable`1+CursorEnumerator.MoveNext (StackExchange.Redis.StrongName, Version=1.0.316.0, Culture=neutral, PublicKeyToken=c219ff1ca8c2ce46)
       at System.Linq.Enumerable+&lt;SelectManyIterator&gt;d__1`2.MoveNext (System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
       at System.Linq.Enumerable+&lt;DistinctIterator&gt;d__1`1.MoveNext (System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
       at System.Linq.Buffer`1..ctor (System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
       at System.Linq.Enumerable.ToArray (System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
       at Test.RedisCacheManager.RemoveByPattern (TestApplication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null)
  </code></pre>
  
  <p>Is there a better way of doing this?
  I know that if I knew about all the keys on the .net side i could avoid the scan. But unfortunately that's not particularly easy in my case.</p>