How to delete blobs from blob storage using Lifecycle management

949 Views Asked by At

I have a storage account and inside that I have container "mycontainer"

I have two virtual folders preview and final

I want to configure life cycle rule to delete all blobs from preview folder which is created a day ago along with that I want to configure another rule that deletes all blobs from final which is created a day ago, only if the blob has an index tag "candelete" : "true"

When I tried configuring Lifecycle rule, it get deletes blobs from preview, but not from final

My rules looks like

{
  "rules": [
    {
      "enabled": true,
      "name": "deletepreview",
      "type": "Lifecycle",
      "definition": {
        "actions": {
          "baseBlob": {
            "delete": {
              "daysAfterCreationGreaterThan": 1
            }
          }
        },
        "filters": {
          "blobTypes": [
            "blockBlob"
          ],
          "prefixMatch": [
            "mycontainer/preview"
          ]
        }
      }
    },
    {
      "enabled": true,
      "name": "deletefinal",
      "type": "Lifecycle",
      "definition": {
        "actions": {
          "baseBlob": {
            "delete": {
              "daysAfterCreationGreaterThan": 1
            }
          }
        },
        "filters": {
          "blobIndexMatch": [
            {
              "name": "candelete",
              "op": "==",
              "value": "true"
            }
          ],
          "blobTypes": [
            "blockBlob"
          ],
          "prefixMatch": [
            "mycontainer/final"
          ]
        }
      }
    }
  ]
}
1

There are 1 best solutions below

1
tanikellav On

Blob Lifecycle Management

I have created blob1 container having t1 and t2 folders as follows

enter image description here

enter image description here

there are two ways to manage blob lifecycle through portal and through code. following method illustrates creating rules through portal

  1. in storage account created go to lifecycle management -> add rules we also have code view where we can edit code

enter image description here

  1. Give Rule name, Rule scope (limit blob as filters), Blob type (Block blobs) , Blob subtype (Base blob) and next

enter image description here

  1. Give conditions as required in this case Base blobs were = Created, days 0(for demonstration), Then = Delete the blob

enter image description here

  1. In next filter set give Blob prefix as container_name/folder_name and click add

enter image description here

  1. For adding filters with Indexed tag make sure to add relevant Blob index tag while creating the file or can be updated after creating the file under the properties

enter image description here

  1. Similarly add another rule with required details and in filter set tab give appropriate Blob prefix and add the key/value under Blob index match then add

enter image description here

  1. The two are created as seen in image

enter image description here

  1. In code view tab the code for the created rules is automatically generated as follows
{

"rules": [

{

"enabled": true,

"name": "deletet1",

"type": "Lifecycle",

"definition": {

"actions": {

"baseBlob": {

"delete": {

"daysAfterCreationGreaterThan": 0

}

}

},

"filters": {

"blobTypes": [

"blockBlob"

],

"prefixMatch": [

"blob1/t1"

]

}

}

},

{

"enabled": true,

"name": "deletet2",

"type": "Lifecycle",

"definition": {

"actions": {

"baseBlob": {

"delete": {

"daysAfterCreationGreaterThan": 0

}

}

},

"filters": {

"blobIndexMatch": [

{

"name": "frp",

"op": "==",

"value": "true"

}

],

"blobTypes": [

"blockBlob"

],

"prefixMatch": [

"blob1/t2"

]

}

}

}

]

}

Note: I noticed that sometimes the files will be deleted after one day, it just takes more than 24 hours to process the rule. In my case as I set the rule to 0, they are deleted within a day.

enter image description here