Last active
August 29, 2015 14:23
-
-
Save tysonmote/20df8fb55a74cd193e9a to your computer and use it in GitHub Desktop.
Delete large Redis sets with Ruby and Sidekiq
This file contains 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 SafeSetDelete | |
include Sidekiq::Worker | |
BATCH_SIZE = 100 | |
# Rename the key and queue for deletion | |
def self.delete(key) | |
newkey = "gc:sets:#{$redis.incr("gc:index")}" | |
$redis.rename(key, newkey) | |
self.perform_async(newkey) | |
end | |
# Delete members from the set in batches | |
def self.perform(key) | |
cursor = "0" | |
loop do | |
cursor, members = $redis.sscan(key, cursor, count: BATCH_SIZE) | |
$redis.srem(key, members) if members.size > 0 | |
break if cursor == "0" | |
end | |
end | |
end | |
# Example: | |
# | |
# SafeSetDelete.delete("my.large.set") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment