One of the most annoying issues with on prem installation (LBD) is when the deployment fails and one of the nastiest bugs is when it happens during a cleanup and requires some surgical removal as retrying results in more failures (warning this fix is not for the faint of heart (at least I don't like deleting apps inside of service fabric manually)).
Connect to the service fabric cluster with the D365FOLBDAdmin module config or manually with the Connect-ServiceFabric.
How I connect to the service fabric cluster with the D365FOLBDAdmin tool is below (Note: you will need to have the Service Fabric Certificate installed on the machine you are running this on)
$D365FOLBDConfig = Get-D365LBDConfig -ComputerName "LBDServer"
Connect-ServiceFabricCluster -connectionEndpoint $D365FOLBDConfig.SFConnectionEndpoint -X509Credential -FindType FindByThumbprint -FindValue $D365FOLBDConfig.SFServerCertificate -ServerCertThumbprint $D365FOLBDConfig.SFServerCertificate
Run below script which will clean up the apps.
$applicationNamesToIgnore = @('fabric:/LocalAgent', 'fabric:/Agent-Monitoring', 'fabric:/Agent-LBDTelemetry')
$applicationTypeNamesToIgnore = @('MonitoringAgentAppType-Agent', 'LocalAgentType', 'LBDTelemetryType-Agent') Get-ServiceFabricApplication | ` Where-Object { $_.ApplicationName -notin $applicationNamesToIgnore } | ` Remove-ServiceFabricApplication -Force
Get-ServiceFabricApplicationType | ` Where-Object { $_.ApplicationTypeName -notin $applicationTypeNamesToIgnore } | ` Unregister-ServiceFabricApplicationType -Force
Go to the WP folder inside of the Agent Share and remove the specific environments folder (if you are concerned you can always just move the folder outside of the WP folder).
Below script will delete the folder (can be done manually if you are more inclined)
$D365FOLBDConfig = Get-D365LBDConfig -ComputerName "LBDServer"
get-childitem $(join-path $D365FOLBDConfig.AgentShareLocation \wp) -Recurse | Remove-item -Recurse
Once executed login to LifeCycle Services (LCS) and hit retry on the deployment. Note this will take a bit longer but should go past the cleanup portion of the deployment scripts.
Still don't have the D365FOLBDAdmin module???
Get the latest version in below locations:

Download from PowerShell Gallery
