Skip to content

Instantly share code, notes, and snippets.

@tysonmote
Last active August 29, 2015 14:23
Show Gist options
  • Save tysonmote/e2d915ef4f8ca4247cea to your computer and use it in GitHub Desktop.
Save tysonmote/e2d915ef4f8ca4247cea to your computer and use it in GitHub Desktop.
Delete large Redis lists with Ruby and Sidekiq
$redis = Redis.new
class SafeListDelete
include Sidekiq::Worker
BATCH_SIZE = 100
# Rename the key and queue for deletion
def self.delete(key)
newkey = "gc:lists:#{$redis.incr("gc:index")}"
$redis.rename(key, newkey)
self.perform_async(newkey)
end
# Trim off elements in batches
def self.perform(key)
while $redis.llen(key) > 0
$redis.ltrim(key, 0, -BATCH_SIZE - 1)
end
end
end
# Example:
#
# SafeListDelete.delete("my.large.list")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment