630

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?


  • I wonder why del * doesn't work. It returns 0. - Chloe

19 답변


1059

With redis-cli:

  • FLUSHDB - Removes data from your connection's CURRENT database.
  • FLUSHALL - Removes data from ALL databases.

Redis documentation:

For example, in your shell:

redis-cli flushall


  • from shell: redis-cli flushall - Cmag
  • When i try the above i get the following error (error) LOADING Redis is loading the dataset in memory. Can you specify why? - Ram Patra
  • @Ramswaroop - you restarted Redis and it is currently loading data from persistent storage. While this process (loading) is active, you can't manipulate the DB. Either wait for it to finish, or configure Redis w/o persistence and restart it (it will start empty so you won't need to do FLUSHALL once it is up). - Itamar Haber
  • @Neo if you don't have a redis client library in C#, you can simply run that command, like so: (new Process { StartInfo = new ProcessStartInfo { FileName = "redis-cli", Arguments = "flushall", CreateNoWindow = true }}).start(); - Christian
  • Use the -h flag to specify a redis server location - Adam F

178

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.


  • +1 from me, i thought this was the better answer actually. Granted, the OP's Question says "wipe everything out" but that is followed by "give me a blank database"--regardless of what he actually meant, i think the distinction you made is useful, to say the least. - doug
  • The answer should read as 'flushall may be overkill..'. - Gokul

26

Answers so far are absolutely correct; they delete all keys.

However, if you also want to delete all Lua scripts from the Redis instance, you should follow it by:

SCRIPT FLUSH

The OP asks two questions; this completes the second question (everything wiped).


  • No, he asked one question and in the two years since then the scope in Redis increased. The OP's question was specifically about deleting keys. You're answering a different question than this one. - The Real Bill
  • @TheRealBill You have to think about what's useful for current and future SO readers. The answers here didn't address the OP's I want everything wiped out and give me a blank database. question anymore. So, imho, my addition is a good one, which has helped a few people. Feel free to disagree ofcourse, that's also what SO is about. - Tw Bert
  • Context is everything, and the first sentence establishes it: "I want to delete all keys". Rewriting the question to give a different answer is not what I'm told SO is about. It is about asking the question given - hence the moderation flag indicating the answer under review is answering a different question. But, opinions differ. - The Real Bill
  • Your opinion is noted, thx. Nuff said. - Tw Bert
  • If you are running the server yourself, the quickest way to wipe everything is to kill the server and restart it (found this out accidentally) - acutesoftware

15

If you're using the redis-rb gem then you can simply call:

your_redis_client.flushdb


11

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.


9

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());
        }
    }

}


6

One more option from my side:

In our production and pre-production databases there are thousands of keys. Time to time we need to delete some keys (by some mask), modify by some criteria etc. Of course, there is no way to do it manually from CLI, especially having sharding (512 logical dbs in each physical).

For this purpose I write java client tool that does all this work. In case of keys deletion the utility can be very simple, only one class there:

public class DataCleaner {

    public static void main(String args[]) {
        String keyPattern = args[0];
        String host = args[1];
        int port = Integer.valueOf(args[2]);
        int dbIndex = Integer.valueOf(args[3]);

        Jedis jedis = new Jedis(host, port);

        int deletedKeysNumber = 0;
        if(dbIndex >= 0){
            deletedKeysNumber += deleteDataFromDB(jedis, keyPattern, dbIndex);
        } else {
            int dbSize = Integer.valueOf(jedis.configGet("databases").get(1));
            for(int i = 0; i < dbSize; i++){
                deletedKeysNumber += deleteDataFromDB(jedis, keyPattern, i);
            }
        }

        if(deletedKeysNumber == 0) {
            System.out.println("There is no keys with key pattern: " + keyPattern + " was found in database with host: " + host);
        }
    }

    private static int deleteDataFromDB(Jedis jedis, String keyPattern, int dbIndex) {
        jedis.select(dbIndex);
        Set<String> keys = jedis.keys(keyPattern);
        for(String key : keys){
            jedis.del(key);
            System.out.println("The key: " + key + " has been deleted from database index: " + dbIndex);
        }

        return keys.size();
    }

}

Writing such kind of tools I find very easy and spend no more then 5-10 min.


5

FLUSHALL Deletes all the Keys of All exisiting databases . FOr Redis version > 4.0 , FLUSHALL ASYNC is supported which runs in a background thread wjthout blocking the server https://redis.io/commands/flushall

FLUSHDB - Deletes all the keys in the selected Database . https://redis.io/commands/flushdb

The time complexity to perform the operations will be O(N) where N being the number of keys in the database.

The Response from the redis will be a simple string "OK"


4

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


4

Use FLUSHALL ASYNC if using (Redis 4.0.0 or greater) else FLUSHALL

https://redis.io/commands/flushall


3

  1. Stop Redis instance.
  2. Delete RDB file.
  3. Start Redis instance.


2

i think sometimes stop the redis-server and delete rdb,aof files。 make sure there’s no data can be reloading. then start the redis-server,now it's new and empty.


2

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

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


1

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')


1

Open redis-cli and type:

FLUSHALL


  • Although your post might answer the question, it lacks some documentation. Please edit your answer and provide those. - hellow

0

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

Its better if you can have RDM (Redis Desktop Manager). You can connect to your redis server by creating a new connection in RDM.

Once its connected you can check the live data, also you can play around with any redis command.

Opening a cli in RDM.

1) Right click on the connection you will see a console option, just click on it a new console window will open at the bottom of RDM.

Coming back to your question FLUSHALL is the command, you can simply type FLUSHALL in the redis cli.

Moreover if you want to know about any redis command and its proper usage, go to link below. https://redis.io/commands.


0

There are different approaches. If you want to do this from remote, issue flushall to that instance, through command line tool redis-cli or whatever tools i.e. telnet, a programming language SDK. Or just log in that server, kill the process, delete its dump.rdb file and appendonly.aof(backup them before deletion).


0

One click in FastoRedis/FastoNoSQL

enter image description here

Linked


Related

Latest