How to delete B2C tenant through PowerShell

Robert Dillon (Azure Admin) 20 Reputation points
2023-02-28T13:18:47.21+00:00

I was able to programmatically delete a B2C tenant through the following PowerShell but it no longer works. The request is listed as 'Accepted' but the tenant is never deleted. I've gone through the delete tenant checklist in the portal so it should be ok. Used this doc for reference https://learn.microsoft.com/en-us/rest/api/activedirectory/b2c-tenants/delete?tabs=HTTP. With the exception of the linked doc, all other documentation points to deleting within the portal. Why would this api call be blocked? Or am I doing something wrong? Thanks

$ApiVersion = "2021-04-01"
$B2CName = "<tenantName>.onmicrosoft.com"
$access_token = Get-AzAccessToken -ResourceUrl "https://management.azure.com/"
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Content-Type", "application/json")
$headers.Add("Authorization", 'Bearer ' + $access_token.Token)
        
$b2cUri = $ManagementAzureUri + "subscriptions/$SubscriptionId/resourceGroups/$ResourceGroupName/providers/Microsoft.AzureActiveDirectory/b2cDirectories/$B2CName" + "?api-version=$ApiVersion"
        
Invoke-RestMethod -Uri $b2cUri -Method 'DELETE' -Headers $headers -Verbose
Windows for business | Windows Server | User experience | PowerShell
Microsoft Security | Microsoft Entra | Microsoft Entra ID
{count} votes

Accepted answer
  1. JamesTran-MSFT 36,911 Reputation points Microsoft Employee Moderator
    2023-03-15T23:33:46.57+00:00

    @Robert Dillon (Azure Admin)

    Thank you for your post and I apologize for the delayed response!

    In order to delete your B2C tenant using PowerShell, please make sure you run the Invoke-RestMethod Delete command as the last step in the deletion process. For more info - How do I delete my Azure AD B2C tenant?

    • When creating my test B2C tenant and trying to delete it right after creation. I didn't realize that I still needed to follow the steps in order to delete my tenant.
    • User's image

    Once I removed the **b2c-extensions-app **and selected "Yes" under the "Access management for Azure resources" Azure AD property, I was able to successfully delete my B2C tenant via PowerShell.

    • User's image
    $SubscriptionId = "<SubID>"
    $ResourceGroupName = "B2C"
    $ManagementAzureUri = "https://management.azure.com/"
    $B2CName = "deleteB2CTenant.onmicrosoft.com"
    
    $ApiVersion = "2021-04-01"
    $access_token = Get-AzAccessToken -ResourceUrl "https://management.azure.com/"
    $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
    $headers.Add("Content-Type", "application/json")
    $headers.Add("Authorization", 'Bearer ' + $access_token.Token)
    
    $b2cUri = $ManagementAzureUri + "subscriptions/$SubscriptionId/resourceGroups/$ResourceGroupName/providers/Microsoft.AzureActiveDirectory/b2cDirectories/$B2CName" + "?api-version=$ApiVersion"
    
    Invoke-RestMethod -Uri $b2cUri -Method 'DELETE' -Headers $headers -Verbose
    

    I hope this helps!


    If the information helped address your question, please Accept the answer. This will help us and also improve searchability for others in the community who might be researching similar information.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.