How do I delete everything in Redis?

前端 未结 22 862
无人共我
无人共我 2020-11-28 17:18

I want to delete all keys. I want everything wiped out and give me a blank database.

Is there a way to do this in Redis client?

相关标签:
22条回答
  • 2020-11-28 17:34

    FLUSHALL Remove all keys from all databases

    FLUSHDB Remove all keys from the current database

    SCRIPT FLUSH Remove all the scripts from the script cache.

    0 讨论(0)
  • Use FLUSHALL ASYNC if using (Redis 4.0.0 or greater) else FLUSHALL.

    https://redis.io/commands/flushall

    Note: Everything before executing FLUSHALL ASYNC will be evicted. The changes made during executing FLUSHALL ASYNC will remain unaffected.

    0 讨论(0)
  • 2020-11-28 17:36

    you can use following approach in python

    def redis_clear_cache(self):
    
        try:
            redis_keys = self.redis_client.keys('*')
        except Exception as e:
            # print('redis_client.keys() raised exception => ' + str(e))
            return 1
    
        try:
            if len(redis_keys) != 0:
                self.redis_client.delete(*redis_keys)
        except Exception as e:
            # print('redis_client.delete() raised exception => ' + str(e))
            return 1
    
        # print("cleared cache")
        return 0
    
    0 讨论(0)
  • 2020-11-28 17:37

    If you are using Java then from the documentation, you can use any one of them based on your use case.

    /**
     * Remove all keys from all databases.
     *
     * @return String simple-string-reply
     */
    String flushall();
    
    /**
     * Remove all keys asynchronously from all databases.
     *
     * @return String simple-string-reply
     */
    String flushallAsync();
    
    /**
     * Remove all keys from the current database.
     *
     * @return String simple-string-reply
     */
    String flushdb();
    
    /**
     * Remove all keys asynchronously from the current database.
     *
     * @return String simple-string-reply
     */
    String flushdbAsync();
    

    Code:

    RedisAdvancedClusterCommands syncCommands = // get sync() or async() commands 
    syncCommands.flushdb();
    

    Read more: https://github.com/lettuce-io/lettuce-core/wiki/Redis-Cluster

    0 讨论(0)
  • 2020-11-28 17:39

    redis-cli -h <host> -p <port> flushall

    It will remove all data from client connected(with host and port)

    0 讨论(0)
  • 2020-11-28 17:42

    Heads up that FLUSHALL may be overkill. FLUSHDB is the one to flush a database only. FLUSHALL will wipe out the entire server. As in every database on the server. Since the question was about flushing a database I think this is an important enough distinction to merit a separate answer.

    0 讨论(0)
提交回复
热议问题