Skip to content

Instantly share code, notes, and snippets.

@szajbus
Last active September 25, 2016 21:39
Show Gist options
  • Save szajbus/72f69b2f7ae94e3fc2eb to your computer and use it in GitHub Desktop.
Save szajbus/72f69b2f7ae94e3fc2eb to your computer and use it in GitHub Desktop.
# Usage:
#
# redis = Redis.new(url: ENV["REDIS_URL"])
# keys = redis.scan_each(match: 'foo*', count: 1_000)
# keys.extend(EachWithProgress)
#
# keys.each_with_progress do |key|
# redis.del(key)
# end
module EachWithProgress
def each_with_progress
status = ''
each_with_index do |item, index|
$stderr.print "\b" * status.length
status = [index+1, size].compact.join('/')
$stderr.print status
yield(item)
end
$stderr.puts
end
end
def del_keys(count, pattern)
redis = Redis.new(url: ENV["REDIS_URL"])
keys = redis.scan_each(match: pattern, count: 1_000)
keys.extend(EachWithProgress)
keys.each_with_progress do |key|
redis.del(key)
break unless (count -= 1) > 0
end
end
@szajbus
Copy link
Author

szajbus commented Apr 13, 2016

del_keys(100_000, "sellektor-web:production:session:*")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment