|
| 1 | +#This PowerShell script helps you permanently delete only the soft-deleted versions of a blob |
| 2 | + |
| 3 | +#Disclaimer |
| 4 | +#By using the following materials or sample code you agree to be bound by the license terms below |
| 5 | +#and the Microsoft Partner Program Agreement the terms of which are incorporated herein by this reference. |
| 6 | +#These license terms are an agreement between Microsoft Corporation (or, if applicable based on where you |
| 7 | +#are located, one of its affiliates) and you. Any materials (other than sample code) we provide to you |
| 8 | +#are for your internal use only. Any sample code is provided for the purpose of illustration only and is |
| 9 | +#not intended to be used in a production environment. We grant you a nonexclusive, royalty-free right to |
| 10 | +#use and modify the sample code and to reproduce and distribute the object code form of the sample code, |
| 11 | +#provided that you agree: (i) to not use Microsoft’s name, logo, or trademarks to market your software product |
| 12 | +#in which the sample code is embedded; (ii) to include a valid copyright notice on your software product in |
| 13 | +#which the sample code is embedded; (iii) to provide on behalf of and for the benefit of your subcontractors |
| 14 | +#a disclaimer of warranties, exclusion of liability for indirect and consequential damages and a reasonable |
| 15 | +#limitation of liability; and (iv) to indemnify, hold harmless, and defend Microsoft, its affiliates and |
| 16 | +#suppliers from and against any third party claims or lawsuits, including attorneys’ fees, that arise or result |
| 17 | +#from the use or distribution of the sample code." |
| 18 | + |
| 19 | +Connect-AzAccount; |
| 20 | +Set-AzContext -SubscriptionId "subscriptionid"; |
| 21 | + |
| 22 | +$storageAccountName = "storageaccountname" |
| 23 | +$resourceGroup = "resourcegroupname" |
| 24 | +$containerName = “conatinername” |
| 25 | + |
| 26 | +$action ="PERMANENT_DELETE" #"PERMANENT_DELETE" #List_Only |
| 27 | + |
| 28 | +$ctx = (Get-AzStorageAccount -ResourceGroupName $resourceGroup -Name $storageAccountName).Context |
| 29 | +$objCount=0 |
| 30 | +$arrDeleted = "" |
| 31 | + |
| 32 | +$objCount = FlatContainerProcessing ($containerName) |
| 33 | + |
| 34 | +function FlatContainerProcessing ($containerName) |
| 35 | +{ |
| 36 | + $blobCount = 0 |
| 37 | + |
| 38 | + $blob_Token = $null |
| 39 | + $exception = $Null |
| 40 | + |
| 41 | + $SASPermissions = 'rwdl' |
| 42 | + |
| 43 | + do |
| 44 | + { |
| 45 | + $listOfBlobs = Get-AzStorageBlob -Container $containerName -IncludeDeleted -IncludeVersion -Context $ctx -ContinuationToken $blob_Token -Prefix $prefix -MaxCount 5000 -ErrorAction Stop |
| 46 | + |
| 47 | + if($listOfBlobs -eq $null) |
| 48 | + { |
| 49 | + break |
| 50 | + } |
| 51 | + |
| 52 | + $listOfDeletedBlobs = $listOfBlobs | Where-Object {($_.IsDeleted -eq $true)} |
| 53 | + |
| 54 | + $listOfDeletedBlobs = $listOfDeletedBlobs | Where-Object {($_.VersionId -ne $null) } # Versions only |
| 55 | + |
| 56 | + $CurrentTime = Get-Date |
| 57 | + $StartTime = $CurrentTime.AddHours(-1.0) |
| 58 | + $EndTime = $CurrentTime.AddHours(59.0) |
| 59 | + |
| 60 | + $sas = New-AzStorageContainerSASToken -Name $containerName -Permission $SASPermissions -StartTime $StartTime -ExpiryTime $EndTime -Context $ctx |
| 61 | + $sas = $sas.Replace("?","") |
| 62 | + |
| 63 | + $blobCount += $listOfDeletedBlobs.Count |
| 64 | + |
| 65 | + foreach($blob in $listOfDeletedBlobs) |
| 66 | + { |
| 67 | + # Creates a table to show the Soft Delete objects |
| 68 | + #-------------------------------------------------- |
| 69 | + if($action -eq "List_Only") |
| 70 | + { |
| 71 | + if($blob.SnapshotTime -eq $null) {$strSnapshotTime = "-"} else {$strSnapshotTime = $blob.SnapshotTime} |
| 72 | + if($blob.VersionID -eq $null) {$strVersionID = "-"} else {$strVersionID = $blob.VersionID} |
| 73 | + |
| 74 | + $arrDeleted = $arrDeleted + ($blob.Name, $blob.Length, $blob.AccessTier, $strSnapshotTime, $strVersionID, $blob.ICloudBlob.Uri.AbsolutePath) |
| 75 | + } |
| 76 | + #---------------------------------------------------------------------- |
| 77 | + |
| 78 | + # Permanent Delete those objects in one call |
| 79 | + #----------------------------------------- |
| 80 | + if($action -eq "PERMANENT_DELETE") |
| 81 | + { |
| 82 | + $sastoken = "account level sas token with permanent delete permission" |
| 83 | + |
| 84 | + $delete_uri = "https://" + $blob.BlobClient.Uri.Host + $blob.BlobClient.Uri.AbsolutePath + "?versionid="+ $blob.VersionID + "&deletetype=permanent&" + $sastoken |
| 85 | + |
| 86 | + Write-Host $delete_uri |
| 87 | + |
| 88 | + try |
| 89 | + { |
| 90 | + $response = Invoke-RestMethod -Method "Delete" -Uri $delete_uri |
| 91 | + } |
| 92 | + catch |
| 93 | + { |
| 94 | + Write-Warning -Message "$_" -ErrorAction Stop |
| 95 | + break |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + $blob_Token = $listOfBlobs[$listOfBlobs.Count -1].ContinuationToken; |
| 101 | + |
| 102 | + }while ($blob_Token -ne $null) |
| 103 | + |
| 104 | + |
| 105 | + if($blobCount -eq 0) |
| 106 | + { |
| 107 | + write-host "No Objects found to list" -ForegroundColor Red |
| 108 | + } |
| 109 | + else |
| 110 | + { |
| 111 | + write-host "Soft Deleted Objects found: $blobCount " -ForegroundColor magenta |
| 112 | + |
| 113 | + if($action -eq 'List_Only') |
| 114 | + { |
| 115 | + if ($blobCount -gt 0) |
| 116 | + { |
| 117 | + $arrDeleted | Format-Wide -Property {$_} -Column 6 -Force | out-string -stream | write-host -ForegroundColor Cyan |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + #write-host "Total objects processed: $blobCount " -ForegroundColor magenta |
| 123 | + return $blobCount |
| 124 | +} |
0 commit comments