5

I am looking for a way to delete stored files in a blob container called reports that are older than 30 days or set any other retention policy that would make sense

As far as I see Microsoft only writes about retention policies for Logs and Metadata. Is this possible? How to achieve that?

Yoda
  • 17,363
  • 67
  • 204
  • 344

1 Answers1

6

You can use Storage Lifecycle policy to achieve that.

#Initialize the following with your resource group and storage account names
$rgname = ""
$accountName = ""

#Create a new action object
$action = Add-AzStorageAccountManagementPolicyAction -BaseBlobAction Delete -daysAfterModificationGreaterThan 2555
$action = Add-AzStorageAccountManagementPolicyAction -InputObject $action -BaseBlobAction TierToArchive -daysAfterModificationGreaterThan 90
$action = Add-AzStorageAccountManagementPolicyAction -InputObject $action -BaseBlobAction TierToCool -daysAfterModificationGreaterThan 30
$action = Add-AzStorageAccountManagementPolicyAction -InputObject $action -SnapshotAction Delete -daysAfterCreationGreaterThan 90

# Create a new filter object
# PowerShell automatically sets BlobType as “blockblob” because it is the only available option currently
$filter = New-AzStorageAccountManagementPolicyFilter -PrefixMatch ab,cd

#Create a new rule object
#PowerShell automatically sets Type as “Lifecycle” because it is the only available option currently
$rule1 = New-AzStorageAccountManagementPolicyRule -Name Test -Action $action -Filter $filter

#Set the policy
$policy = Set-AzStorageAccountManagementPolicy -ResourceGroupName $rgname -StorageAccountName $accountName -Rule $rule1

https://learn.microsoft.com/en-us/azure/storage/blobs/storage-lifecycle-management-concepts?tabs=azure-portal#add-or-remove-a-policy

4c74356b41
  • 69,186
  • 6
  • 100
  • 141
  • 1
    In 3 minutes you came up with this much code :). Awesome! – Gaurav Mantri Jan 24 '20 at 12:45
  • I've applied that but haven't saw effects yet. How often are these rules executed? – Yoda Jan 24 '20 at 12:57
  • 1
    `I created a new policy, why do the actions not run immediately? The platform runs the lifecycle policy once a day. Once you configure a policy, it can take up to 24 hours for some actions to run for the first time.` – Yoda Jan 24 '20 at 13:16