How to Set Lifecycle Management for Azure Blob Storage
To set lifecycle management for blobs in Azure, create a JSON policy defining rules for transitioning or deleting blobs based on their age or conditions, then apply it to your storage account using the Azure CLI or Azure Portal. Use the
az storage account management-policy create command with a policy file to automate blob lifecycle actions.Syntax
The lifecycle management policy for Azure Blob Storage is a JSON file with a rules array. Each rule has a name, enabled status, type (usually 'Lifecycle'), definition with filters to select blobs, and actions to specify what happens (e.g., delete, move to cool or archive tier).
Key parts:
- filters: Define which blobs the rule applies to (by prefix, blob types, or tags).
- actions: Define lifecycle actions like
delete,tierToCool, ortierToArchivewith conditions likedaysAfterModificationGreaterThan.
json
{
"rules": [
{
"name": "rule-name",
"enabled": true,
"type": "Lifecycle",
"definition": {
"filters": {
"blobTypes": ["blockBlob"],
"prefixMatch": ["container-name/prefix"]
},
"actions": {
"baseBlob": {
"delete": {
"daysAfterModificationGreaterThan": 30
},
"tierToCool": {
"daysAfterModificationGreaterThan": 10
}
}
}
}
}
]
}Example
This example shows how to create a lifecycle management policy that moves blobs to the cool tier after 30 days and deletes them after 365 days. It uses Azure CLI to apply the policy to a storage account.
json
{
"rules": [
{
"name": "move-to-cool-and-delete",
"enabled": true,
"type": "Lifecycle",
"definition": {
"filters": {
"blobTypes": ["blockBlob"]
},
"actions": {
"baseBlob": {
"tierToCool": {
"daysAfterModificationGreaterThan": 30
},
"delete": {
"daysAfterModificationGreaterThan": 365
}
}
}
}
}
]
}Output
Policy applied successfully to the storage account.
Common Pitfalls
- Not enabling the rule (
"enabled": false) means the policy won't run. - Incorrect
prefixMatchfilters can exclude blobs unintentionally. - Using wrong blob types in
blobTypescan cause rules to not apply. - Applying policy to the wrong storage account or forgetting to specify the resource group.
- Not waiting enough time to see lifecycle actions take effect, as they run once a day.
azure
Wrong example (rule disabled):
{
"rules": [
{
"name": "disabled-rule",
"enabled": false,
"type": "Lifecycle",
"definition": { ... }
}
]
}
Correct example (rule enabled):
{
"rules": [
{
"name": "enabled-rule",
"enabled": true,
"type": "Lifecycle",
"definition": { ... }
}
]
}Quick Reference
Use the Azure CLI command below to set the lifecycle policy JSON file policy.json on your storage account:
az storage account management-policy create --account-name <storage-account-name> --resource-group <resource-group> --policy @policy.json
Remember:
- Policy JSON must have
rulesarray. - Each rule needs
name,enabled,type,definition. - Filters select blobs; actions define lifecycle steps.
Key Takeaways
Create a JSON lifecycle policy with rules to automate blob tiering and deletion.
Use Azure CLI with the policy file to apply lifecycle management to your storage account.
Ensure rules are enabled and filters correctly target your blobs.
Lifecycle actions run once daily, so changes take time to apply.
Test policies on non-critical data to avoid accidental deletion.