Automate builds and clean-ups with ACR tasks

DevOps, Hands On20 minutesCosts moneyWritten 14 November 2026By Cloud Hands On

In this task you'll run builds and clean-ups on a schedule.

An ACR task is a saved job in the registry. It can build from a Git repository on a timer, when the base image changes, or on a commit, and it can run commands such as acr purge to delete old images.

Tag scheduled builds with {{.Run.ID}} so each one is unique, and always try a purge with --dry-run first.

Example. Fernhill Cloud rebuilds every team's images nightly to pick up base image patches, and purges tags older than 30 days every Sunday, keeping the newest five.

Costs money. This task creates resources that are charged while they exist. Delete them as soon as you finish.

Before you start

You need: A container registry with ACR Tasks allowed. Time: 20 minutes.

Where to look

  1. Home
  2. Container registries
  3. (your registry)
  4. Services
  5. Tasks

Steps

  1. Create a nightly build: az acr task create -r <name> -n nightly --context <git url> --file Dockerfile --image 'app:{{.Run.ID}}' --schedule '0 2 * * *' --commit-trigger-enabled false.
  2. Run it once: az acr task run -r <name> -n nightly.Proves it works before you trust the schedule.
  3. Dry-run a purge: az acr run -r <name> --cmd "acr purge --filter 'app:.*' --ago 30d --keep 5 --untagged --dry-run" /dev/null.
  4. Schedule the real purge with az acr task create ... --cmd "acr purge ..." --schedule '0 3 * * Sun' --context /dev/null.

Commands

az acr task create -r <name> -n weekly-purge --cmd "acr purge --filter 'sskaiser/shop:.*' --ago 30d --keep 5 --untagged" --schedule '0 3 * * Sun' --context /dev/null
az acr task list-runs -r <name> -o table

Watch out for

Cost
Billed per second of task run time: pennies. Free trial subscriptions block ACR Tasks (error TasksOperationsNotAllowed), so az acr build, az acr run and scheduled tasks fail until you upgrade to pay-as-you-go or ask Azure support to allow them. Importing and locking tags still work.
Security
Commit and pull request triggers need a GitHub token: store it carefully, or use timers instead.
Common mistakes
Forgetting --keep (so a quiet repository loses every image), and skipping the dry run.

Clean up

Delete the tasks (az acr task delete) or the registry's resource group.

Words to know

Timer trigger
A cron schedule on a task, in UTC.
acr purge
A command that deletes tags older than a set age, and untagged manifests.
{{.Run.ID}}
A placeholder replaced by the task run's unique ID.

Tick off each step and track your progress in the guide.

Open this task in DevOps, Hands On

Keep going