I plan to backup my azure vhd files by shutting down my vm and then copying the vhd files from the production container to a backup container. How can I automate deleting vhd files that are a week old in the backup container?
If you can accept using PowerShell, then this would do it for you. It will register a scheduled job to run daily and remove PageBlob's in the container specified.
$taskTrigger = New-ScheduledTaskTrigger -Daily -At 12:01AM
Register-ScheduledJob -Name DeleteMyOldFiles -Trigger $taskTrigger -ScriptBlock {
$isOldDate = [DateTime]::UtcNow.AddDays(-7)
Get-AzureStorageBlob -Container "[YOUR CONTAINER NAME]" |
Where-Object { $_.LastModified.UtcDateTime -lt $isOldDate -and $_.BlobType -eq "PageBlob" } |
Remove-AzureStorageBlob
}
This is something not available right out of the box. You would have to write some code yourself. Essentially the steps would be:
LastModifiedDate
(this would be in UTC).A few other things:
PageBlob
)I found this answer when trying to delete the whole container. Using Rick's answer as a template I came up with this trial what-if to determine which containers would be deleted:
$ctx = New-AzureStorageContext -StorageAccountName $AzureAccount `
-StorageAccountKey $AzureAccountKey
$isOldDate = [DateTime]::UtcNow.AddDays(-7)
Get-AzureStorageContainer -Context $ctx `
| Where-Object { $_.LastModified.UtcDateTime -lt $isOldDate } `
| Remove-AzureStorageContainer -WhatIf
Then when I was satisfied that the list should be deleted, I used -Force
so as to not have to confirm every delete:
Get-AzureStorageContainer -Context $ctx `
| Where-Object { $_.LastModified.UtcDateTime -lt $isOldDate } `
| Remove-AzureStorageContainer -Force
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With