This is expected behavior when you are using the CLI command for webconfig as CLI exposes different command delete, list, set, therefore the delete and set retain the existing configuration.
If you are using PowerShell then there is no such command that will retain the value. Therefore you need to first get the app setting using the GetAzWebApp command and then call the Set-AzWebApp to update the settings. Any addition, removal of the app setting you need to pass the object again that overwritten all the existing settings. If you are not passing the existing values then it will only retain the new app settings.
For PowerShell I have tested the below script and it is working as expected.
$appset = (Get-AzWebApp -Name 'webappname' -ResourceGroupName 'resourcegroupname').SiteConfig.AppSettings
$newsettings = new-object Microsoft.Azure.Management.WebSites.Models.NameValuePair
$newsettings.Name = "key"
$newsettings.Value = "value"
$appset.Add($newsettings)
$newappset = @{}
$appset | ForEach-Object {
$newappset[$_.Name] = $_.Value
}
Set-AzWebApp -AppSettings $newappset -Name webappname-ResourceGroupName resourcegroupname
Similarly, when you are using Microsoft.Azure.Management.WebSites package then you need to call ListApplicationSettings or ListApplicationSettingsAsyn first to get the application settings and then make the changes to the object (add/delete) and then pass that object to UpdateApplicationSettingsAsync method as we have done in the above PowerShell script.
Hope the above helps you to resolve the issue. Feel free to reach out to me if you need any assistance.
Please 'Accept as answer' and ‘Upvote’ if it helped so that it can help others in the community looking for help on similar topics.