title: Deleting multiple keys in a redis cluster
body: <p>As I understand in redis cluster the slots are distributed, the <code>DEL</code> command which can take multiple keys fails with a <code>CROSSSLOT Keys in request don't hash to the same slot</code> error. </p>
  
  <p>Since my existing codebase is designed around batch processing, there are quite a few places where the redis cache delete operation is called with a list of keys to be deleted. As changing this would not be a smart idea <strong>I am trying to find a solution at my cache interface layer where I can still receive the multiple keys to be deleted and add required logic here to achieve the same final result.</strong> I had two options in my view which I need help with </p>
  
  <p><strong>Method 1.</strong> Loop on the keys and use Go routines + weight groups </p>
  
  <p><strong>Method 2.</strong> Use <code>EVAL</code> : I am not sure if this is the right direction or if I am using it right. Following is the sample golang snippet below </p>
  
  <pre class="lang-golang prettyprint-override"><code>func (c CacheClient) Del(ctx context.Context, keysToBeDeleted []string) error {
      // _, err := c.client.Del(strings.Join(keysToBeDeleted, " ")).Result() // previously used when on single redis instance
      _, err := c.client.Eval("return redis.call('DEL', KEYS[1])", keysToBeDeleted).Result()
  
      if err != nil {
          // handle error
      }
      return nil
  }
  </code></pre>
  
  <p>I created a dummy cluster and used redis-cli to test the eval command as below </p>
  
  <p><code>redis&gt; EVAL "redis.call('del', KEYS[1]) " 2 mykey1, mykey2</code></p>
  
  <p>This too failed with a <code>CROSSSLOT Keys in request don't hash to the same slot</code> error. </p>
  
  <p><strong>Am I using the KEYS table in a wrong way in the Lua script. Or what is the right way to delete multiple keys from a cluster. Should I stick to method 1?</strong> </p>
  
  <p>Using Golang v1.13 and <a href="https://godoc.org/gopkg.in/redis.v5" rel="nofollow noreferrer">redis.v5</a> as my redis-client package. 
  If someone can help me find a solution using the redis.v5 package itself that would be even great. </p>