Clearing Azure Redis Cache using PowerShell during deployment

6.6k views Asked by At

When deploying new versions of our web application to Azure App Service, I have a requirement to clear out the data in the associated Azure Redis Cache. This is to ensure that we don't return old versions of items which have schema changes in the new version.

We're deploying using Octopus Deploy, and I have previously tried executing the following PowerShell command to Reset the cache:

Reset-AzureRmRedisCache -ResourceGroupName "$ResourceGroup" -Name "$PrimaryCacheName" -RebootType "AllNodes" -Force

This works successfully but it's a bit heavy-handed and we're having intermittent connection issues which I suspect are caused by the fact that we're rebooting Redis and dropping existing connections.

Ideally, I'd just like to execute a FLUSHALL command via PowerShell. Is this a better approach, and is it possible to execute in PowerShell using the StackExchange.Redis library?

3

There are 3 answers

2
juvchan On BEST ANSWER

The Reset-AzureRmRedisCache cmdlet restarts nodes of an Azure Redis Cache instance, which I agree it is a bit overkill for your requirement.

Yes, it is possible to execute a Redis FLUSHALL command in PowerShell.

As the pre-requisite, you should install the Redis CLI and set an environment variable to point to the Redis CLI executable/binary path in your environment.

Then, you can execute in PowerShell using the Redis-CLI commands as shown below.

Invoke-Command -ScriptBlock { redis-cli -h <hostname>.redis.cache.windows.net -p <redisPort> -a <password> }
Invoke-Command -ScriptBlock { redis-cli flushall }

A execution result of code sample above is as shown below: enter image description here

1
TallMcPaul On

The way I eventually implemented this is to call the StackExchange.Redis library via PowerShell, so you'll need to have a copy of this DLL somewhere handy. During my deployment, I have access to the connection string, so this function strips out the host and port to connect to the server. This works without the need to open the non-SSL port, and the connection string allows admin access to the cache:

function FlushCache($RedisConnString)
{
   # Extract the Host/Port from the start of the connection string (ignore the remainder)
   # e.g. MyUrl.net:6380,password=abc123,ssl=True,abortConnect=False
   $hostAndPort = $RedisConnString.Substring(0, $RedisConnString.IndexOf(","))

   # Split the Host and Port e.g. "MyUrl.net:6380" --> ["MyUrl.net", "6380"]
   $RedisCacheHost, $RedisCachePort = $hostAndPort.split(':')

   Write-Host "Flushing cache on host - $RedisCacheHost - Port $RedisCachePort" -ForegroundColor Yellow

   # Add the Redis type from the assembly
   $asm = [System.Reflection.Assembly]::LoadFile("StackExchange.Redis.dll") 

   # Open a connection
   [object]$redis_cache = [StackExchange.Redis.ConnectionMultiplexer]::Connect("$RedisConnString,allowAdmin=true",$null)

   # Flush the cache
   $redisServer = $redis_cache.GetServer($RedisCacheHost, $RedisCachePort,$null)
   $redisServer.FlushAllDatabases()

   # Dispose connection
   $redis_cache.Dispose()

   Write-Host "Cache flush done" -ForegroundColor Yellow
}
0
jaeyow On

I have used the Windows port of netcat to clear Redis cache remotely from my Windows machine, like so:

$redisCommands = "SELECT $redisDBIndex`r`nFLUSHDB`r`nQUIT`r`n"
$redisCommands | .\nc $redisServer 6379

Where $redisDBIndex is the Redis Cache index you want to clear. Or simply the command FLAUSHALL if you want to clear everything. $redisServer is your Redis server. And simply pipe to nc.

I have also documented it here: https://jaeyow.github.io/fullstack-developer/automate-redis-cache-flush-in-powershell/#