You might want to take a look at the different options to delete the images from ACR by following the below document:
https://learn.microsoft.com/en-us/azure/container-registry/container-registry-delete
Also take a look at automatically purging:
https://learn.microsoft.com/en-us/azure/container-registry/container-registry-auto-purge
All those above options should help you out in writing the script or using the purge or you can use a batch script which was mentioned in the document or finally you can make use of the Function App timed trigger to schedule the batch script !!
Kindly go through all the commands mentioned in that document to start with & understand the difference and significance of untagging or removing or purging etc !
Kindly let us know if you have any additional follow-up questions , happy to help out !
Regards,
Shiva.