Deleting Large Hashes in Redis

To learn more about why deleting large objects is slow in Redis, read this quick overview

To delete a large hash in Redis:

  1. Rename the key to a unique, namespaced key so that the hash appears “deleted” to other Redis clients immediately.

  2. Incrementally delete fields from the hash in small batches until it is empty. By limiting the size of our delete commands, we ensure that we don’t block the server for too long.

Please note that the following code doesn’t gracefully handle Redis connection failures. If any Redis command fails and raises an exception, you’ll need to clean up manually.

Pseudo-code

# Rename the key
newkey = "gc:hashes:" + redis.INCR( "gc:index" )
redis.RENAME("my.hash.key", newkey)

# Delete fields from the hash in batche of 100s
cursor = 0
loop
  cursor, hash_keys = redis.HSCAN(newkey, cursor, "COUNT", 100)
  if hash_keys count > 0
    redis.HDEL(newkey, hash_keys)
  end
  if cursor == 0
    break
  end
end

Ruby

$redis = Redis.new

def delete_hash(key)
  # Rename the key
  newkey = "gc:hashes:#{$redis.incr("gc:index")}"
  $redis.rename(key, newkey)

  # Delete fields from the hash in batches of 100
  cursor = "0"
  loop do
    cursor, fields = $redis.hscan(newkey, cursor, count: 100)
    hkeys = fields.map { |pair| pair[0] }
    $redis.hdel(newkey, hkeys) if hkeys.size > 0
    break if cursor == "0"
  end
end

# Example:
#
#   delete_hash("my.large.hash")

Here are some example implementations of the above using background jobs in Ruby:

← Back to “Deleting Large Objects in Redis”

Last updated 09 Jun 2015. Originally written by Tyson Mote

← Back to docs