197

I've been playing with redis (and add some fun with it) during the last fews days and I'd like to know if there is a way to empty the db (remove the sets, the existing key....) easily.
During my tests, I created several sets with a lot of members, even created sets that I do not remember the name (how can I list those guys though ?).
Any idea about how to get rid of all of them ?

Luc
  • 16,604
  • 34
  • 121
  • 183

6 Answers6

236

You have two options:

  • FLUSHDB - clears currently active database
  • FLUSHALL - clears all the existing databases
plaes
  • 31,788
  • 11
  • 91
  • 89
146

Be careful here.

FLUSHDB deletes all keys just in the current database (0 by default), while FLUSHALL deletes all keys in all databases (on the current host).

mirekphd
  • 4,799
  • 3
  • 38
  • 59
Dexter
  • 11,311
  • 11
  • 45
  • 61
  • Is there any downside to using FlushDB to clear a specific database from production code? – Alex Naspo Nov 10 '14 at 21:21
  • 1
    @AlexNaspo potentially problematic since the bigger your DB is, the longer it will take to flush. Since numbered (shared) databases are managed by the same Redis server, this could block your other databases for that duration. If possible, refrain from using shared databases because of that and also because they aren't future-proof (not supported in v3). See here for more details: https://redislabs.com/blog/benchmark-shared-vs-dedicated-redis-instances – Itamar Haber Nov 10 '14 at 23:49
  • @ItamarHaber What is the most efficient way to delete or expire a set of keys that match a specific format? – Alex Naspo Nov 11 '14 at 03:21
  • 1
    @AlexNaspo if you're using v2.8+, a combo of SCAN and DEL would do the trick nicely. See here for a bash script that does it: http://stackoverflow.com/a/23399125/3160475 – Itamar Haber Nov 11 '14 at 09:09
  • `$ redis-cli` then select database. I am selecting 0 `> select 0` and delete all keys of db 0 `> FLUSHDB` – sagar junnarkar May 05 '15 at 17:41
63

tldr: flushdb clears one database and flushall clears all databases

Clear CURRENT

Delete default or currently selected database (usually 0) with

redis-cli flushdb

Clear SPECIFIC

Delete specific redis database with (e.g. 8 as my target database):

redis-cli -n 8 flushdb 

Clear ALL

Delete all redis databases with

redis-cli flushall
Marc
  • 4,820
  • 3
  • 38
  • 36
36

With redis-cli:

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

Redis Docs: FLUSHDB, FLUSHALL

Hieu Le
  • 2,106
  • 1
  • 23
  • 23
4

There are right answers but I just want to add one more option (requires downtime):

  1. Stop Redis.
  2. Delete RDB file (find location in redis.conf).
  3. Start Redis.
Denys
  • 1,308
  • 12
  • 13
0

open your Redis cli and There two possible option that you could use:

FLUSHDB - Delete all the keys of the currently selected DB. FLUSHALL - Delete all the keys of all the existing databases, not just the currently selected one.

behzad babaei
  • 1,111
  • 10
  • 11