19

I am using StackExchange.Redis in my application to store key/values. I need to flush the entire db now which Redis is using. I found a way via command How do I delete everything in Redis? but how can I do this with StackExchange.Redis? I was not able to find any method for that?

I searched for Empty, RemoveAll etc on IDatabase object and found nothing.

Community
  • 1
  • 1
Raghav
  • 8,772
  • 6
  • 82
  • 106

1 Answers1

33

The easiest way is to use FlushDatabase method or FlushDatabaseAsync from IServer

ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost,allowAdmin=true");
var server = redis.GetServer("localhost");
server.FlushDatabase();
Bohdan Stupak
  • 1,455
  • 8
  • 15
  • 1
    Is there a way to do this via some command line tool? I'd hate to have to write code just to flush the cache during development... – cesar-moya Feb 13 '18 at 19:38
  • 1
    sure, you can do this in via redis-cli.exe with the argument "flushall" to flush all data in redis or you can you "flushdb" just to flush the selected db. e.g. if you need to flush db 1 you the next commands - "select 1" to select db with index 1, "flushdb" to flush only selected db. please note, the default db index is 0 – Arkhangelskiy Evgeniy Feb 14 '18 at 07:50
  • @cesar-moya you can also use the "Redis Desktop Manager" (free windows app) see: https://redisdesktop.com/download – bytedev Apr 16 '18 at 13:59
  • 1
    Is says "This operation is not available unless admin mode is enabled: FLUSHALL". – Gopu_Tunas Aug 06 '19 at 10:05
  • 5
    Make sure you set allowAdmin=true in the config string (or ConfigurationOptions). – Arkhangelskiy Evgeniy Aug 06 '19 at 14:04