info@techdevops.com
TechDevOps.com
Explore Tools
Experts in Microsoft SQL Server Operations, Performance, Replication, Cloud & Migrations



Azure Powershell - Delete all files in Storage Files Share
by BF (Senior MSSQL Operations - Enterprise, Cloud, Strategy)
2019-04-02









Azure Powershell - Delete all files in Storage Files Share:


#Install-Module -Name Az

#Update-Module -Name Az


$StorageAccount = 'azrstg01'

$StorageAccountKey = ''

$ctx = New-AzureStorageContext -StorageAccountName $StorageAccount -StorageAccountKey $StorageAccountKey


# List all files older than x hrs

$fromDate = [DateTime]::UtcNow.AddHours(-24)

$files = Get-AzureStorageFile -ShareName "sqlbackups" -Context $ctx | get-azurestoragefile | where {$_.GetType().Name -eq "CloudFile"}

foreach($file in $files) {

$file.FetchAttributes();

if ($file.Properties.LastModified.UtcDateTime -lt $fromDate) {

$file.Name

}

}


# Remove all files older than x hrs

$fromDate = [DateTime]::UtcNow.AddHours(-720)

$files = Get-AzureStorageFile -ShareName "sqlbackups" -Context $ctx | get-azurestoragefile | where {$_.GetType().Name -eq "CloudFile"}

foreach($file in $files) {

$file.FetchAttributes();

if ($file.Properties.LastModified.UtcDateTime -lt $fromDate) {

#$file.Name

$filename = "Backups/" + $file.Name

#Get-AzureStorageFile -ShareName "sqlbackups" -Path "Backup" -Context $ctx

Remove-AzureStorageFile -Context $ctx -ShareName "sql-backup" -Path $filename

}

}