Skip to content

Instantly share code, notes, and snippets.

@tysonmote
Created June 16, 2015 23:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tysonmote/8c9f4bae25aa8b57dbf3 to your computer and use it in GitHub Desktop.
Save tysonmote/8c9f4bae25aa8b57dbf3 to your computer and use it in GitHub Desktop.
Delete large Redis sorted sets with Ruby and Sidekiq
$redis = Redis.new
class SafeSortedSetDelete
include Sidekiq::Worker
BATCH_SIZE = 100
# Rename the key and queue for deletion
def self.delete(key)
newkey = "gc:zsets:#{$redis.incr("gc:index")}"
$redis.rename(key, newkey)
self.perform_async(newkey)
end
# Delete members from the sorted set in batches
def self.perform(key)
while $redis.zcard(key) > 0
$redis.zremrangebyrank(key, 0, BATCH_SIZE - 1)
end
end
end
# Example:
#
# SafeSortedSetDelete.delete("my.large.zset")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment