Created
July 7, 2016 14:43
-
-
Save tgwizard/1a33878c5dea4a2c28631f057ef9d189 to your computer and use it in GitHub Desktop.
Delete multiple redis keys. Safe to run in production
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
# -*- coding: utf-8 -*- | |
from __future__ import print_function | |
import os | |
import sys | |
import itertools | |
from redis import StrictRedis | |
redis_url = os.environ.get('REDIS_URL') | |
assert redis_url, u'You must specify the REDIS_URL environment variable' | |
client = StrictRedis.from_url(redis_url) | |
key_matches = sys.argv[1:] | |
assert len(key_matches) > 0, u'You must specify at least one key match to delete' | |
print(u'Will delete key matching: %s' % key_matches) | |
for key_match in key_matches: | |
it = client.scan_iter(match=key_match, count=1000) | |
while True: | |
keys = list(itertools.islice(it, 1000)) | |
if not keys: | |
print('\ndone: %s' % key_match) | |
break | |
client.delete(*keys) | |
sys.stdout.write('.') | |
sys.stdout.flush() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment