Search code examples
pythonredisgoogle-cloud-memorystore

Get list of keys in managed Redis | Google Cloud Memory Store


I'm working on Cloud Memory Store (documentation) for storing data in the Redis using python client library.

Python client library has various function to add, set, get, delete values (for more info), but I couldn't find anything to get the list of values that are already been stored in the database.

My use case is to fetch a chunk of keys(based on specific prefix) and delete them.

I want to know how to get the list of keys that are already stored in the database.


Solution

  • Here is a python snippet using scan() to get all keys from the store matching a pattern and delete them one-by-one:

    import redis
    r = redis.StrictRedis(host='localhost', port=6379, db=0)
    for key in r.keys('*'):
        # delete the key
        r.delete(key)