Reputation: 11
I want to delete the records from azure storage table based on some condtion. I have column like datemodified in azure storage table. My query will looks like
datemodifed < (Get-Date).ToUniversalTime().AddDays(-10)
Can anyone tell me how to remove the records one by one based on above condition using powershell script. Thanks in advance
Upvotes: 0
Views: 4227
Reputation: 86
Use this command:
$entityToDelete | Remove-AzTableRow -table $cloudTable
To get the $cloudTable you need to get the storage account's and entity's context first. For example you can use this script:
$storageAccountName = "MyStorage"
$resourceGroup = "MyRG"
$tableName = "MyTable"
$columnName = "MyColum"
$value = "EntityValueToSearch"
$storageAccount = Get-AzStorageAccount `
-ResourceGroupName $resourceGroup `
-Name $storageAccountName
$ctx = $storageAccount.Context
$storageTable = Get-AzStorageTable –Name $tableName –Context $ctx
$cloudTable = ($storageTable).CloudTable
[string]$filter = `
[Microsoft.Azure.Cosmos.Table.TableQuery]::GenerateFilterCondition($columnName,`
[Microsoft.Azure.Cosmos.Table.QueryComparisons]::Equal,$value)
# Get entity
$entityToDelete = Get-AzTableRow `
-table $cloudTable `
-customFilter $filter
$entityToDelete | Remove-AzTableRow -table $cloudTable
For more info and a good referance: https://learn.microsoft.com/en-us/azure/storage/tables/table-storage-how-to-use-powershell#deleting-table-entities
Upvotes: 2
Reputation: 2354
Working with Azure Storage Tables from PowerShell
TableOperation.Delete accepts a Table Entity. See here: https://msdn.microsoft.com/en-us/library/microsoft.windowsazure.storage.table.tableoperation.delete.aspx
Look into TableQuery class here https://msdn.microsoft.com/en-us/library/microsoft.windowsazure.storage.table.tablequery_methods.aspx
Reference :https://www.wintellect.com/deleting-entities-in-windows-azure-table-storage/
Upvotes: -1