Skip to content

Instantly share code, notes, and snippets.

@tgwizard
Created July 7, 2016 14:43
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 tgwizard/1a33878c5dea4a2c28631f057ef9d189 to your computer and use it in GitHub Desktop.
Save tgwizard/1a33878c5dea4a2c28631f057ef9d189 to your computer and use it in GitHub Desktop.
Delete multiple redis keys. Safe to run in production
# -*- 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