sandeep kumar
sandeep kumar

Reputation: 11

How to delete the rows from azure table using powershell script

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

Answers (2)

Yuval Lahav
Yuval Lahav

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

Related Questions