Search code examples
azureterraformterraform-provider-azureterragrunt

How to delete Containers in Azure Storage account with the creation date


I need to delete Containers in Azure Storage account with the creation date of more than 1 year as we create container for every month.

I have tired using Azure Storage Lifecycle management , but we are not able to delete container but we can delete inside files.

Is there a way to delete container itself from Azure / Terraform ?

I want to delete the highlighted container automatically Via terraform .

Azure portal

enter image description here


Solution

  • Deleting a container inside the storage account based on time of creation/modification using terraform.

    This requirement mentioned is not directly possible using terraform as it doesn't support the functionality based on time stats.

    In order to achieve this requirement, we need use CLI commands for this which has the privilege to fetch the metadata info of the resource. If you still want to use terraform to achieve this, you can use null resource.

    Initially I tested to delete the containers which are 2 days old as shown below

    enter image description here

    Configuration:

    variable "resource_group_name" {
      description = "Resource group name"
      type        = string
      default     = "vinay-rg"
    }
    
    variable "storage_account_name" {
      description = "Storage account name"
      type        = string
      default     = "testsamoeksas"
    }
    
    resource "null_resource" "delete_old_containers" {
      provisioner "local-exec" {
        interpreter = ["/bin/bash", "-c"]
        command = <<EOT
          #!/bin/bash
    
          storageAccount="${var.storage_account_name}"
          cutoffDate=$(date -d '-2 days' +"%Y-%m-%dT%H:%M:%SZ")  # here is tried to set this for 2 days ago containerss
    
         
          delete_if_old() {
              local containerName=$1
    
              # Get the last modified date of the container
              lastModified=$(az storage container show \
                  --name $containerName \
                  --account-name $storageAccount \
                  --auth-mode login \
                  --query properties.lastModified \
                  --output tsv)
    
              if [[ -z "$lastModified" ]]; then
                  echo "Error: Could not retrieve the last modified date for container: $containerName"
                  return
              fi
    
              
              lastModifiedEpoch=$(date -d "$lastModified" +%s)
              cutoffDateEpoch=$(date -d "$cutoffDate" +%s)
    
              # Compare lastModified date with the cutoff date (2 days ago)
              if [[ $lastModifiedEpoch -lt $cutoffDateEpoch ]]; then
                  echo "Deleting container: $containerName (Last Modified: $lastModified)"
                  az storage container delete \
                      --name $containerName \
                      --account-name $storageAccount \
                      --auth-mode login
              else
                  echo "Skipping container: $containerName (Last Modified within the last 2 days)"
              fi
          }
    
          
          list=$(az storage container list \
              --query "[].name" \
              --account-name $storageAccount \
              --auth-mode login \
              --output tsv)
    
          for containerName in $list; do
              delete_if_old $containerName
          done
        EOT
      }
    }
    

    Deployment:

    enter image description here

    enter image description here

    Refer:

    https://learn.microsoft.com/en-us/azure/storage/blobs/blob-containers-cli#delete-containers

    https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/storage_container