Skip to content

PowerShell script that bulk-deletes files older than a chosen age in an Azure File Share—handles millions of items, walks sub-folders, supports parallel deletes, and includes a -WhatIf dry-run for safety.

License

Notifications You must be signed in to change notification settings

travishankins/azure-file-purge

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 

Repository files navigation

🧹 Azure File Share Purge Script

Purge-AzFileShare.ps1

Deletes files older than N days (optionally within a specific sub-folder) from an Azure File Share.


✨ Highlights

Feature
🚀 Scales to tens of millions of objects – streams 5 000 entries/page & handles continuation tokens
🌳 Recursive by default; optionally start lower via -StartPath
Parallel deletes (configurable) for high throughput
🔍 -WhatIf mode prints paths without deleting
♻️ Resume-safe – rerun any time; already-deleted files are skipped
🔐 Works with Shared Key or Azure AD / Managed Identity authentication

🛠️ Prerequisites

Requirement Notes
PowerShell 7+ Windows · macOS · Linux · Azure Cloud Shell
Azure CLI 2.60+ Script shells out to az storage file …
List / Delete permission Either:
• Shared Key
or Azure roles:
  Storage File Data SMB Share Contributor
  Storage File Data Privileged Contributor

⚡ Quick start (local workstation)

1 — 🗝️ Grab a storage-account key

az login
az account set --subscription "<SUBSCRIPTION-GUID>"

az storage account keys list \
  --resource-group  <RESOURCE-GROUP> \
  --account-name    <STORAGE-ACCOUNT> \
  --query "[0].value" -o tsv

Copy the 88-character string.

2 — 🔑 Export credentials

export AZURE_STORAGE_ACCOUNT=<STORAGE-ACCOUNT>
export AZURE_STORAGE_KEY=<PASTE-KEY-HERE>
# PowerShell users:
# $env:AZURE_STORAGE_ACCOUNT = '<STORAGE-ACCOUNT>'
# $env:AZURE_STORAGE_KEY     = '<PASTE-KEY-HERE>'

3 — 🧪 Dry-run the script

./Purge-AzFileShare.ps1 `
  -ResourceGroupName  <RESOURCE-GROUP> `
  -StorageAccountName $Env:AZURE_STORAGE_ACCOUNT `
  -ShareName          <FILE-SHARE> `
  -Days               45 `
  -StartPath          ''      # or 'Folder/SubFolder' to scope lower
  -WhatIf             # preview only

Remove -WhatIf once the preview looks correct.


⚙️ Parameters

Parameter Required Default Description
-ResourceGroupName Resource group that owns the storage account
-StorageAccountName Storage account hosting the share
-ShareName File-share to purge
-Days 30 Delete files older than N days
-PageSize 5000 Objects per list page (service max)
-MaxConcurrent 32 Parallel delete workers
-StartPath '' Folder to begin recursion (blank = root)
-WhatIf Dry-run; no deletes executed

⏰ Scheduling options

Platform How to wire it
Azure Automation Import as PS 7 runbook → store key in secure variables → schedule 03:00 UTC daily
GitHub Actions Save key in Secrets → CRON 0 3 * * *pwsh ./Purge-AzFileShare.ps1 …
Task Scheduler pwsh -File Purge-AzFileShare.ps1 … with nightly trigger; load env vars in wrapper .bat

🤝 Contributing

PRs welcome! Ideas:

  • Retry / back-off logic
  • Exclusion patterns / globbing
  • Output to CSV or Log Analytics

About

PowerShell script that bulk-deletes files older than a chosen age in an Azure File Share—handles millions of items, walks sub-folders, supports parallel deletes, and includes a -WhatIf dry-run for safety.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published