How do I delete everything in Redis?

前端 未结 22 863
无人共我
无人共我 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:42

    This method worked for me - delete everything of current connected Database on your Jedis cluster.

    public static void resetRedis() {
        jedisCluster = RedisManager.getJedis(); // your JedisCluster instance
    
        for (JedisPool pool : jedisCluster.getClusterNodes().values()) {
    
            try (Jedis jedis = pool.getResource()) {
                jedis.flushAll();
            }
            catch (Exception ex){
                System.out.println(ex.getMessage());
            }
        }
    
    }
    
    0 讨论(0)
  • 2020-11-28 17:46

    After you start the Redis-server using:service redis-server start --port 8000 or redis-server.

    Use redis-cli -p 8000 to connect to the server as a client in a different terminal.

    You can use either

    1. FLUSHDB - Delete all the keys of the currently selected DB. This command never fails. The time-complexity for this operation is O(N), N being the number of keys in the database.
    2. FLUSHALL - Delete all the keys of all the existing databases, not just the currently selected one. This command never fails. The time-complexity for this operation is O(N), N being the number of keys in all existing databases.

    Check the documentation for ASYNC option for both.

    If you are using Redis through its python interface, use these two functions for the same functionality:

    def flushall(self):
        "Delete all keys in all databases on the current host"
        return self.execute_command('FLUSHALL')
    

    and

    def flushdb(self):
        "Delete all keys in the current database"
        return self.execute_command('FLUSHDB')
    
    0 讨论(0)
  • 2020-11-28 17:47

    This works for me: redis-cli KEYS \* | xargs --max-procs=16 -L 100 redis-cli DEL

    It list all Keys in redis, then pass using xargs to redis-cli DEL, using max 100 Keys per command, but running 16 command at time, very fast and useful when there is not FLUSHDB or FLUSHALL due to security reasons, for example when using Redis from Bitnami in Docker or Kubernetes. Also, it doesn't require any additional programming language and it just one line.

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

    You can use FLUSHALL which will delete all keys from your every database. Where as FLUSHDB will delete all keys from our current database.

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

    Open redis-cli and type:

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

    Your questions seems to be about deleting entire keys in a database. In this case you should try:

    1. Connect to redis. You can use the command redis-cli (if running on port 6379), else you will have to specify the port number also.
    2. Select your database (command select {Index})
    3. Execute the command flushdb

    If you want to flush keys in all databases, then you should try flushall.

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