Simon C
Simon C

Reputation: 158

Remove Custom Domain/Host from App Service via Powershell

I am unable to find a way to remove a host name from a azure web app/app service.

I have tried to use the following filtering our unwanted hosts, but nothing is removed.

Set-AzureWebsite -Name "<<name>>" -HostNames $hosts

and

Set-AzureRmWebApp -Name "<<name>>" -ResourceGroupName "<<name>>" -HostNames $hosts

I have around 200 hosts to delete, however, I can't seem to find an automated way of doing it.

Upvotes: 0

Views: 2736

Answers (2)

Christian
Christian

Reputation: 21

Just an update AzureRm has now been replaced with Az so the statement would now be

$webApp = Get-AzWebApp -ResourceGroupName "<Resource-Group-Name>" -Name "<App-Name>";
$webApp.HostNames.Clear();
$webApp.Hostnames.Add($webApp.DefaultHostName);
Set-AzWebApp `
  -ResourceGroupName "<Resource-Group-Name>" `
  -Name <App-Name> `
  -HostNames $webApp.HostNames;

Upvotes: 2

Byron Tardif
Byron Tardif

Reputation: 1182

at a top level this is what you need to do:

  1. Get the websites resource
  2. Manipulate the hostnames collection
  3. Post the changes back to azure

Here is an example of how I did it:

$webApp = Get-AzureRmWebApp -ResourceGroupName "<<Resource-Group-Name>>" -Name "<<App_Name>>"
$webApp.HostNames.Clear()
$webApp.Hostnames.Add($webApp.DefaultHostName)
set-AzureRmWebApp -ResourceGroupName "<<Resource-Group-Name>>" -Name <<App_Name>> -HostNames $webApp.HostNames

This will remove all custom hostnames and leave only the default one.

If you want to remove a specific hostname for the collection you could use:

$webApp.HostNames.Remove("your_hostname_goes_here")

NOTE If your hostname has SSL bindings you will need to remove those first and then delete the hostname.

Upvotes: 5

Related Questions