To learn more about why deleting large objects is slow in Redis, read this quick overview
To delete a large list in Redis:
Rename the key to a unique, namespaced key so that the list appears “deleted” to other Redis clients immediately.
Incrementally delete elements from the list 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.
# Rename the key
newkey = "gc:hashes:" + redis.INCR("gc:index")
redis.RENAME("my.list.key", newkey)
# Trim off elements in batche of 100s
while redis.LLEN(newkey) > 0
redis.LTRIM(newkey, 0, -99)
end
$redis = Redis.new
def delete_list(key)
# Rename the key
newkey = "gc:lists:#{$redis.incr("gc:index")}"
$redis.rename(key, newkey)
# Trim off elements in batches of 100
while $redis.llen(newkey) > 0
$redis.ltrim(newkey, 0, -99)
end
end
# Example:
#
# delete_list("my.large.list")
Here are some example implementations of the above using background jobs in Ruby: