Search code examples
azure-service-fabric

Force delete of Service Fabric application


I am having issues during development where an exception during service start up causes the application to get stuck in an error state. This prevents further debugging. I can't seem to delete the application through the cluster management portal either, it just times out.

The only way I've found to fix is to reset the cluster, which takes a few minutes and deletes all other applications from the cluster as well.

What's the correct way to force delete of an individual application?


Solution

  • I created the following powershell script which seems to do the trick:

    param([string]$applicationName = "SomeAppName", [string]$version = "1.0.0")
    
    $applicationUri = "fabric:/" + $applicationName
    $applicationTypeName = $applicationName + "Type"
    
    Connect-ServiceFabricCluster localhost:19000
    
    Write-Host ""
    Write-Host "Removing $applicationUri from local cluster"
    
    Remove-ServiceFabricApplication -ApplicationName fabric:/$applicationName
    
    Write-Host ""
    Write-Host "Removing $applicationTypeName (v$version) from local cluster"
    
    Unregister-ServiceFabricApplicationType -ApplicationTypeName $applicationTypeName -ApplicationTypeVersion $version
    
    Write-Host ""
    Write-Host "Complete"
    

    Can be called like so:

    .\Delete-SF-App.ps1 -applicationName "MyService" -version "1.0.0"
    

    EDIT:

    If the above doesn't work then the below powershell command can be used to remove individual replicas:

    $nodes = Get-ServiceFabricNode
    
    foreach($node in $nodes)
    {
        $replicas = Get-ServiceFabricDeployedReplica -NodeName $node.NodeName -ApplicationName $applicationName
        foreach ($replica in $replicas)
        {
            Remove-ServiceFabricReplica -ForceRemove -NodeName $node.NodeName -PartitionId $replica.Partitionid -ReplicaOrInstanceId $replica.ReplicaOrInstanceId
        }
    }