Purge-AzFileShare.ps1
Deletes files older than N days (optionally within a specific sub-folder) from an Azure File Share.
| 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 |
| 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 |
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 tsvCopy the 88-character string.
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>'./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 onlyRemove -WhatIf once the preview looks correct.
| 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 |
| 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 |
PRs welcome! Ideas:
- Retry / back-off logic
- Exclusion patterns / globbing
- Output to CSV or Log Analytics