info@techdevops.com | 437-991-3573 | Data Engineering Services
TechDevOps.com
Resources Tools
Experts in Microsoft SQL Server on Windows, Linux, Containers | Clusters, Always On, FCI | Migrations, Cloud, Performance



Azure Powershell - Delete all files in Storage Files Share
by BF (Principal Consultant; Architecture; Engineering)
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

}

}