'AzPowerShell Script Task in Deployment Pipeline. What are some of the ways to resolve a Conflict error when Removing and Create an Az Storage Table
I have an Azure DevOps Deployment Pipeline with an Azure PowerShell Script Task.
The script task populates an Azure Storage Table. It gets the data from a json file.
Unfortunately, my client keeps on changing their mind with what they want in the table.
So, I thought the best thing to do would be to delete the table, recreate it and repopulate it with the new data.
But, I get a conflict error if I run the Remove and Create back-to-back. Which makes sense since it seems so much of Azure uses a messaging architecture.
Anyway, does anyone have a solution to this issue? I have a few, but they seem jerry-rigged.
Here a subset of the codde:
$storageAccount = Get-AzStorageAccount -ResourceGroupName $ResourceGroupName -AccountName $StorageAccountName
$storageTable = Get-AzStorageTable `
-Name $tableName `
-Context $storageAccount.Context `
-ErrorVariable ev `
-ErrorAction SilentlyContinue
if($storageTable) {
Remove-AzStorageTable -Name $tableName -Context $storageAccount.Context -Force
}
New-AzStorageTable -Name $tableName -Context $storageAccount.Context
$storageTable = Get-AzStorageTable -Name $tableName -Context $storageAccount.Context
Any suggestions would be appreciated.
Solution 1:[1]
I have done a few modifications to your code. Please follow the below snippet
$storageAccount = Get-AzStorageAccount -ResourceGroupName $ResourceGroupName -AccountName $StorageAccountName
$storageTable = Get-AzStorageTable `
-Name $tableName `
-Context $storageAccount.Context `
-ErrorVariable ev `
-ErrorAction SilentlyContinue
# if you dont have the table it will gives the $ev so you have to capture this and if table is not there we have to create
if ($ev) {
New-AzStorageTable -Name $tableName -Context $storageAccount.Context
$isDeleted = $false
write-host("Table is not there it is created")
}
# if table exist you can remove the table
elseif($storageTable.Name -eq $tableName) {
Remove-AzStorageTable -Name $tableName -Context $storageAccount.Context
$isDeleted = $true
write-host("Table is there so it is Removed")
# After deleting table we have to wait for 40 sec to completely delete the table from Garbage. Mean while we cannot access the table storage.
Start-Sleep -Seconds 40
}
if($isDeleted -eq $true)
{
New-AzStorageTable -Name $tableName -Context $storageAccount.Context
write-host("Table is Removed so it is created")
}
$storageTable = Get-AzStorageTable -Name $tableName -Context $storageAccount.Context
After Deleting/Removing a table it is immediately marked for deletion and is no longer accessible to clients is likely to take at least
40 secondsto complete on that time it will be in a Garbage collection. Refer here
Results:

Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | DelliganeshS-MT |
