We have an Azure Devops release that builds our repo, packages it, and deploys it to our Azure Batch service with the following PowerShell script:
New-AzureRmBatchApplicationPackage -AccountName "$(BatchAccountName)" -ResourceGroupName "$(ResourceGroupName)" -ApplicationId "$(ApplicationId)" -ApplicationVersion "$(Build.BuildNumber)-$(Release.ReleaseId)" -Format zip -FilePath "$(System.DefaultWorkingDirectory)/_artifact/artifact/bin/theapplication.zip"
Set-AzureRmBatchApplication -AccountName "$(BatchAccountName)" -ResourceGroupName "$(ResourceGroupName)" -ApplicationId "$(ApplicationId)" -DefaultVersion "$(Build.BuildNumber)-$(Release.ReleaseId)"
$Context = Get-AzureRmBatchAccount -AccountName "$(BatchAccountName)" -ResourceGroupName "$(ResourceGroupName)"
Get-AzureBatchComputeNode -PoolId "$(PoolId)" -BatchContext $Context | Restart-AzureBatchComputeNode -BatchContext $Context
The problem is, this doesn't seem to "clean up" old versions of the application package, and I don't see a way in the API documentation to do so. So every few weeks I get this sort of error message:
The maximum allowed number of application packages have already been added for the specified application.
How can I change this script to deploy the application and clean up old versions so I don't have periodic failures in my deployment?