Search code examples
azureredisazure-powershellstackexchange.redisredis-cli

Clearing Azure Redis Cache using PowerShell during deployment


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?


Solution

  • 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