Hello SafiyullahSA,
Welcome to the Microsoft Q&A and thank you for posting your questions here.
I understand that you would like to manage instances in Azure VMSS Autoscaling.
To automatically delete stopped instances and update autoscaling rules in Azure Virtual Machine Scale Sets (VMSS), you can use a combination of autoscaling configurations and custom scale-in policies. Autoscaling can be set up to automatically adjust the number of VM instances based on CPU usage or other metrics. If you have VMs that are stopped, they won't contribute to the CPU usage, but they may still exist within the scale set. You can configure a custom scale-in policy to remove stopped or idle VMs by using options such as "OldestVM" or "NewestVM" to dictate which VMs should be removed first.
To achieve this, you can modify the autoscale configuration using the Azure CLI, PowerShell, or the Azure portal, ensuring that your scaling policies are in place and that your VMSS is using the appropriate orchestration mode (default is now "Flexible"). Additionally, you can manually trigger deletion of stopped instances via scripting using commands like az vmss update
or configure autoscaling rules to handle it automatically based on performance thresholds.
For a clearer picture, to control which instance is deleted, it's recommended to know the stopped instance first, then delete the stopped instance. Then, you can update the autoscaling configuration, ensuring the VMSS retains only the active instances you intend to keep. For more detailed guidance on implementing these policies and configuration options:
- https://learn.microsoft.com/en-us/azure/virtual-machine-scale-sets/virtual-machine-scale-sets-autoscale
- https://learn.microsoft.com/en-us/azure/virtual-machine-scale-sets/scale-in-policy
Also, utilize the documentation by the right side of this page for further reading.
I hope this is helpful! Do not hesitate to let me know if you have any other questions.
Please don't forget to close up the thread here by upvoting and accept it as an answer if it is helpful.