Last active
August 29, 2015 14:23
-
-
Save tysonmote/95dd2efa8c93fc0370c6 to your computer and use it in GitHub Desktop.
Delete large Redis hashes with Ruby and Sidekiq
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$redis = Redis.new | |
class SafeHashDelete | |
include Sidekiq::Worker | |
BATCH_SIZE = 100 | |
# Rename the key and queue for deletion | |
def self.delete(key) | |
newkey = "gc:hashes:#{$redis.incr("gc:index")}" | |
$redis.rename(key, newkey) | |
self.perform_async(newkey) | |
end | |
# Delete fields from the hash in batches | |
def self.perform(key) | |
cursor = "0" | |
loop do | |
cursor, fields = $redis.hscan(key, cursor, count: BATCH_SIZE) | |
hkeys = fields.map { |pair| pair[0] } | |
$redis.hdel(key, hkeys) if hkeys.size > 0 | |
break if cursor == "0" | |
end | |
end | |
end | |
# Example: | |
# | |
# SafeHashDelete.delete("my.large.hash") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment