Strategy to unprovision old applicationtype versions in Service Fabric Strategy to unprovision old applicationtype versions in Service Fabric powershell powershell

Strategy to unprovision old applicationtype versions in Service Fabric


There are two options that cross my mind -

  • Specify UnregisterUnusedApplicationVersionsAfterUpgrade = $true when you execute Deploy-FabricApplication.ps1. This parameter indicates whether to unregister any unused application versions that exist after an upgrade is finished.

  • Add custom script into your release defintion, deployment script or whereever you want that will resolve all the deployed app types and unprovision those ones that you think are obsolete. Here is the command that you will need to use - Unregister-ServiceFabricApplicationType. Here is some example of the script that unregisters all the app types except running ones -

    #resolve all app types$appTypes = Get-ServiceFabricApplicationTypeforeach($appType in $appTypes){   #try to find the match with any of installed applications   $match = Get-ServiceFabricApplication -ApplicationTypeName $appType.ApplicationTypeName | Where-Object {$_.ApplicationTypeVersion -eq $appType.ApplicationTypeVersion}   if(!$match)   {       Write-Host "Deleting $($appType.ApplicationTypeName) $($appType.ApplicationTypeVersion)"       Unregister-ServiceFabricApplicationType -ApplicationTypeName $appType.ApplicationTypeName -ApplicationTypeVersion $appType.ApplicationTypeVersion -Force -Confirm   }    }


You can set CleanupUnusedApplicationTypes to true in the Management section of cluster settings to enable automatic cleanup. From the docs:

This configuration if enabled, allows to automatically unregister unused application type versions skipping the latest three unused versions, thereby trimming the disk space occupied by image store. The automatic cleanup will be triggered at the end of successful provision for that specific app type and also runs periodically once a day for all the application types. Number of unused versions to skip is configurable using parameter "MaxUnusedAppTypeVersionsToKeep".

I believe this requires at least Service Fabric 6.5.


I expanded on Kiyrl's answer keeping the currently deployed version + n history'

#resolve all app types$appTypes = Get-ServiceFabricApplicationType$deployedAppArray = @()foreach($appType in $appTypes){        #try to find the match with any of installed applications    $match = Get-ServiceFabricApplication -ApplicationTypeName $appType.ApplicationTypeName | Where-Object {$_.ApplicationTypeVersion -eq $appType.ApplicationTypeVersion}    if(!$match)    {        $oldApp = new-object psobject -property @{            ApplicationTypeName = $appType.ApplicationTypeName            ApplicationTypeVersion  = $appType.ApplicationTypeVersion         }        $deployedAppArray += $oldApp    }    }$countToKeep = 2 # keeps this many in addition + currently deployed$uniqueAppTypes = $deployedAppArray | Group-Object "ApplicationTypeName" | Where-Object { $_.Count -gt $countToKeep } | Select-Object -ExpandProperty Nameforeach($appType in $uniqueAppTypes){     $versionsToRemove = $deployedAppArray | Where-Object {$_.ApplicationTypeName -eq $appType}    $toRemoveCount =  $versionsToRemove.Length - $countToKeep    $versionsToRemove = $versionsToRemove | Select-Object -First $toRemoveCount    foreach($appToRemove in $versionsToRemove){        Write-Host "Removing $($appToRemove.ApplicationTypeName) $($appToRemove.ApplicationTypeVersion)"        Unregister-ServiceFabricApplicationType -ApplicationTypeName $appToRemove.ApplicationTypeName -ApplicationTypeVersion $appToRemove.ApplicationTypeVersion -Force    }}