Recursive Delete Azure Management Groups
bicep templates that create Azure Management Groups, a recursive removal of chosen management group and all of it’s children is a much needed tool.
Sifting through the documentation of the Az.Resources module, such thing does not seams to exist. And after a quick search I found a post on Stack Overflow that I copied and changed to my needs.
To use the function, copy the code and run it in powershell or dot-source it.
Then call the function with the parameter -ManagementGroupId set to the ID of the management group you recursively want to delete.
Remove-AzManagementGroupRecursive -ManagementGroupId "company-root-mg" -WhatIf
-KeepParentManagementGroupwhen used, the parent management group is saved and NOT deleted.
-MoveSubscriptionif there are any subscriptions contained in the management groups being deleted, this will move them to the root management group.
-ResetDefaultManagementGroupif the default management group is one of the groups being deleted, this will set it to the root management group.
AND always test and verify before removing any management groups,
use the usual parameters of:
to get all needed information.