Recursive delete Azure Management groups

When testing 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.

1
Remove-AzManagementGroupRecursive -ManagementGroupId "company-root-mg" -WhatIf

Parameters

  • -KeepParentManagementGroup when used, the parent management group is saved and NOT deleted.
  • -MoveSubscription if there are any subscriptions contained in the management groups being deleted, this will move them to the root management group.
  • -ResetDefaultManagementGroup if 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:

  • -WhatIf
  • -Verbose

to get all needed information.


Related Posts