This cheatsheet offers commands and configuration snippets to manage the lifecycle of data in storage systems. It covers setting TTL for Cosmos DB, configuring log retention, and automating data cleanup. It is designed for DevOps professionals focused on resource management and cost optimization.
az cosmosdb collection create --collection-name MyCollection --db-name MyDatabase --resource-group <rg> --name <cosmosAccountName> --ttl 3600
SELECT * FROM MyCollection c WHERE c._ts < (UnixTimestamp() - 3600)
Using Azure Functions trigger on TTL expiry.
// Configure Azure Function to trigger deletion of expired items.
az storage account management-policy create --account-name <accountName> --resource-group <rg> --policy @"policy.json"
(policy.json should contain retention rules)
az monitor diagnostic-settings create --name "RetentionPolicy" --resource <resourceId> --logs '[{"category": "AuditLogs", "enabled": true, "retentionPolicy": {"days": 30, "enabled": true}}]'
az monitor log-analytics query --workspace "<workspaceId>" --query "AzureDiagnostics | where TimeGenerated > ago(7d)" --output table
az storage blob delete-batch --source <containerName> --pattern "*.*" --if-unmodified-since "2022-01-01T00:00:00Z"
az monitor metrics list --resource <storageResourceId> --metric "Transactions" --output table
az deployment group create --resource-group <rg> --template-file ttlTemplate.json
az monitor diagnostic-settings list --resource <resourceId> --output table