Repository: angular
Issue: Control flow schematics: doesn't remove the <ng-template #somevar> after replacing it with @else
Labels: area: migrations,core: control flow

### Which @angular/* package(s) are the source of the bug?

core

### Is this a regression?

Yes

### Description

upon running command `ng generate @angular/core:control-flow` for the below html file

```
<ng-container *ngIf="($any(to?.options) || []).length; else default">
  <ng-container *ngTemplateOutlet="checkboxListTmpl"></ng-container>
</ng-container>

<ng-template #checkboxListTmpl>
  <ng-container *ngIf="!to?.grouped; else checkboxGroupTmpl">
    <trq-checkbox
      *ngFor="let option of (to?.options | trqForceTypeCasting); let i = index;"
      [attr.id]="option?.value || null"
      [inline]="to?.config?.inline"
      [checked]="formControl.value && formControl.value[option.key]"
      [formlyAttributes]="field"
      [name]="to?.fieldLabel"
      [disabled]="to?.disabled | trqTypeCasting"
      (checkedChange)="onChange(option.key, $event)"
      >{{option?.label}}</trq-checkbox
    >
  </ng-container>
</ng-template>

<ng-template #default>
  <trq-checkbox
    #formFieldTarget
    [attr.id]="id"
    [inline]="to?.config?.inline"
    [checked]="formControl?.value"
    [formControl]="formControl | trqTypeCasting"
    [formlyAttributes]="field"
    [required]="to?.required | trqForceTypeCasting"
    >{{to?.label}}</trq-checkbox
  >
</ng-template>

<ng-template #checkboxGroupTmpl>
  <trq-checkbox-group
    #formFieldTarget
    [attr.id]="id"
    [inline]="to?.config?.inline"
    [formControl]="formControl | trqTypeCasting"
    [formlyAttributes]="field"
    [required]="to?.required | trqForceTypeCasting">
    <trq-checkbox
      *ngFor="let option of (to?.options | trqForceTypeCasting);"
      [id]="option"
      [disabled]="to?.disabled | trqTypeCasting"
      >{{option}}</trq-checkbox
    >
  </trq-checkbox-group>
</ng-template>

```

i am getting the following response - 

```
@if (($any(to?.options) || []).length) {

<ng-container *ngTemplateOutlet="checkboxListTmpl"></ng-container>

} @else {

<trq-checkbox
  #formFieldTarget
  [attr.id]="id"
  [inline]="to?.config?.inline"
  [checked]="formControl?.value"
  [formControl]="formControl | trqTypeCasting"
  [formlyAttributes]="field"
  [required]="to?.required | trqForceTypeCasting"
  >{{to?.label}}</trq-checkbox
>

}

<ng-template #checkboxListTmpl>
  @if (!to?.grouped) { @for (option of (to?.options | trqForceTypeCasting); track option; let i = $index) {
  <trq-checkbox
    [attr.id]="option?.value || null"
    [inline]="to?.config?.inline"
    [checked]="formControl.value && formControl.value[option.key]"
    [formlyAttributes]="field"
    [name]="to?.fieldLabel"
    [disabled]="to?.disabled | trqTypeCasting"
    (checkedChange)="onChange(option.key, $event)"
    >{{option?.label}}</trq-checkbox
  >
  } } @else {

  <trq-checkbox-group
    #formFieldTarget
    [attr.id]="id"
    [inline]="to?.config?.inline"
    [formControl]="formControl | trqTypeCasting"
    [formlyAttributes]="field"
    [required]="to?.required | trqForceTypeCasting">
    <trq-checkbox
      *ngFor="let option of (to?.options | trqForceTypeCasting);"
      [id]="option"
      [disabled]="to?.disabled | trqTypeCasting"
      >{{option}}</trq-checkbox
    >
  </trq-checkbox-group>

  }
</ng-template>

<ng-template #checkboxGroupTmpl>
  <trq-checkbox-group
    #formFieldTarget
    [attr.id]="id"
    [inline]="to?.config?.inline"
    [formControl]="formControl | trqTypeCasting"
    [formlyAttributes]="field"
    [required]="to?.required | trqForceTypeCasting">
    @for (option of (to?.options | trqForceTypeCasting); track option) {
    <trq-checkbox [id]="option" [disabled]="to?.disabled | trqTypeCasting">{{option}}</trq-checkbox>
    }
  </trq-checkbox-group>
</ng-template>
```


the template - <ng-template #checkboxGroupTmpl> is still there which is now not required, i have similar issues in other files too


### Please provide a link to a minimal reproduction of the bug

_No response_

### Please provide the exception or error you saw

```true
No Error
```


### Please provide the environment you discovered this bug in (run `ng version`)

```true
"dependencies": {
    "@angular/animations": "17.0.0",
    "@angular/cdk": "^16.2.12",
    "@angular/common": "17.0.0",
    "@angular/compiler": "17.0.0",
    "@angular/core": "17.0.0",
    "@angular/forms": "17.0.0",
    "@angular/platform-browser": "17.0.0",
    "@angular/platform-browser-dynamic": "17.0.0",
    "@angular/router": "17.0.0",
    "@hyperviewhq/angular2gridster": "16.0.0",
    "@ngneat/content-loader": "^7.0.0",
    "@ngrx/store": "16.3.0",
    "@ngx-formly/core": "6.1.1",
    "@ngx-translate/core": "~14.0.0",
    "@nx/angular": "17.0.3",
    "@nx/plugin": "17.0.3",
    "@swimlane/ngx-charts": "^20.4.1",
    "@torque/clampjs": "1.0.6",
    "@torque/clr-angular": "5.0.0-trq.105",
    "@torque/clr-core": "5.0.0-trq.105",
    "@torque/clr-ui": "5.0.0-trq.105",
    "@torque/ng2-date-picker": "13.0.61",
    "@torque/tinymce": "6.4.0-trq.21",
    "@types/d3-array": "^3.0.8",
    "@types/d3-axis": "^3.0.4",
    "@types/d3-format": "^3.0.2",
    "@types/d3-geo": "^3.0.5",
    "@types/d3-scale": "^4.0.5",
    "@types/d3-selection": "^3.0.7",
    "@types/d3-transition": "^3.0.5",
    "@types/d3-zoom": "^3.0.5",
    "@types/element-resize-detector": "^1.1.4",
    "@types/lodash": "4.14.182",
    "@types/lodash-es": "^4.17.9",
    "@types/masonry-layout": "^4.2.5",
    "@types/mousetrap": "^1.6.12",
    "@types/topojson-client": "^3.1.2",
    "@types/webfontloader": "^1.6.35",
    "angular-resizable-element": "^7.0.2",
    "angular-split": "^16.2.1",
    "angular-svg-round-progressbar": "^11.0.0",
    "angularx-qrcode": "^16.0.2",
    "d3-axis": "^3.0.0",
    "d3-format": "^3.1.0",
    "d3-geo": "^3.1.0",
    "d3-scale": "^4.0.2",
    "d3-selection": "^3.0.0",
    "d3-zoom": "^3.0.0",
    "date-fns": "^2.30.0",
    "date-fns-tz": "^2.0.0",
    "element-resize-detector": "^1.2.4",
    "fuse.js": "^6.6.2",
    "intl": "^1.2.5",
    "lodash-es": "4.17.21",
    "marked": "^4.3.0",
    "masonry-layout": "^4.2.2",
    "moment": "^2.29.4",
    "mousetrap": "^1.6.5",
    "mutationobserver-shim": "^0.3.7",
    "ng-recaptcha": "^12.0.2",
    "ng2-nouislider": "^2.0.0",
    "ngx-clipboard": "^16.0.0",
    "ngx-color-picker": "^13.0.0",
    "ngx-dynamic-hooks": "^2.1.0",
    "ngx-image-cropper": "7.1.0",
    "ngx-infinite-scroll": "^16.0.0",
    "ngx-markdown": "^16.0.0",
    "ngx-ui-tour-core": "11.0.5",
    "normalize-scroll-left": "^0.2.1",
    "nouislider": "^15.7.1",
    "perfect-scrollbar": "1.5.5",
    "popper.js": "^1.16.1",
    "prismjs": "^1.29.0",
    "purecss": "^3.0.0",
    "resize-observer-polyfill": "^1.5.0",
    "rxjs": "~7.8.0",
    "scroll-into-view-if-needed": "^3.1.0",
    "seamless": "^1.4.1",
    "swiper": "10.3.1",
    "text-mask-core": "^5.0.0",
    "tinycolor2": "^1.6.0",
    "topojson-client": "^3.1.0",
    "tree-model": "^1.0.7",
    "tslib": "^2.3.0",
    "webfontloader": "^1.6.28",
    "what-input": "^5.2.12",
    "zone.js": "0.13.3"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "16.2.10",
    "@angular-devkit/core": "16.2.10",
    "@angular-devkit/schematics": "16.2.10",
    "@angular-eslint/eslint-plugin": "16.3.1",
    "@angular-eslint/eslint-plugin-template": "16.3.1",
    "@angular-eslint/template-parser": "16.3.1",
    "@angular/cli": "^16.2.10",
    "@angular/compiler-cli": "17.0.0",
    "@angular/language-service": "17.0.0",
    "@babel/core": "~7.22.19",
    "@babel/plugin-proposal-decorators": "^7.23.2",
    "@babel/preset-env": "^7.22.15",
    "@babel/preset-typescript": "^7.22.15",
    "@commitlint/cli": "^17.0.2",
    "@commitlint/config-conventional": "^17.0.2",
    "@commitlint/prompt-cli": "^17.0.0",
    "@custom-elements-manifest/analyzer": "~0.5.7",
    "@mjhenkes/postcss-rtl": "^2.0.0",
    "@nx/cypress": "17.0.3",
    "@nx/eslint": "17.0.3",
    "@nx/eslint-plugin": "17.0.3",
    "@nx/js": "17.0.3",
    "@nx/storybook": "17.0.3",
    "@nx/workspace": "17.0.3",
    "@open-wc/testing": "^3.1.6",
    "@rollup/plugin-alias": "^4.0.0",
    "@rollup/plugin-node-resolve": "^15.0.1",
    "@rollup/plugin-terser": "^0.4.3",
    "@rollup/plugin-typescript": "^11.0.0",
    "@rollup/plugin-virtual": "^3.0.1",
    "@schematics/angular": "16.2.8",
    "@storybook/addon-a11y": "7.5.3",
    "@storybook/addon-essentials": "7.5.3",
    "@storybook/angular": "7.5.3",
    "@storybook/blocks": "7.4.1",
    "@storybook/components": "7.4.1",
    "@storybook/core-events": "7.4.1",
    "@storybook/core-server": "7.5.3",
    "@storybook/manager-api": "7.4.1",
    "@storybook/preview-api": "7.4.1",
    "@storybook/theming": "7.4.1",
    "@storybook/types": "7.4.1",
    "@storybook/web-components": "^7.5.1",
    "@storybook/web-components-webpack5": "7.5.3",
    "@torque/eslint-plugin": "12.0.0-build.25",
    "@types/cors": "^2.8.6",
    "@types/csso": "^5.0.0",
    "@types/express": "^4.17.13",
    "@types/jasmine": "~4.3.0",
    "@types/jasminewd2": "~2.0.9",
    "@types/marked": "^4.3.0",
    "@types/morgan": "^1.9.2",
    "@types/node": "^13.7.7",
    "@types/ramda": "^0.28.12",
    "@types/request": "^2.48.4",
    "@types/tinycolor2": "^1.4.6",
    "@typescript-eslint/eslint-plugin": "5.62.0",
    "@typescript-eslint/parser": "5.62.0",
    "@web/dev-server-esbuild": "^0.3.0",
    "@web/test-runner": "^0.13.31",
    "autoprefixer": "^10.4.16",
    "cem-plugin-readonly": "^0.0.5",
    "cem-plugin-vs-code-custom-data-generator": "^1.4.1",
    "chalk": "^4.1.1",
    "conventional-changelog-cli": "^4.1.0",
    "cors": "^2.8.5",
    "cross-env": "^7.0.3",
    "csso": "^5.0.0",
    "eslint": "8.52.0",
    "eslint-config-prettier": "9.0.0",
    "eslint-plugin-import": "2.29.0",
    "eslint-plugin-jasmine": "^4.1.0",
    "eslint-plugin-jsdoc": "46.8.2",
    "eslint-plugin-prefer-arrow": "1.2.3",
    "eslint-plugin-rxjs": "5.0.3",
    "eslint-plugin-storybook": "^0.6.15",
    "execa": "^5.1.1",
    "express": "^4.18.1",
    "git-grep": "^0.1.0",
    "glob": "^8.0.3",
    "husky": "^8.0.3",
    "jasmine": "^5.0.0",
    "jasmine-core": "~4.5.0",
    "jasmine-marbles": "^0.9.2",
    "jasmine-spec-reporter": "7.0.0",
    "karma": "~6.4.0",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage": "~2.2.0",
    "karma-jasmine": "~5.1.0",
    "karma-jasmine-html-reporter": "~2.0.0",
    "karma-spec-reporter": "^0.0.36",
    "karma-teamcity-reporter": "^1.1.0",
    "lint-staged": ">=14.0.1",
    "lit": "^2.2.6",
    "lit-html": "^2.2.0",
    "morgan": "^1.10.0",
    "ncp": "^2.0.0",
    "ng-mocks": "^14.11.0",
    "ng-packagr": "16.2.3",
    "npm-run-all": "^4.1.5",
    "nx": "17.0.3",
    "nx-stylelint": "15.0.0",
    "patch-package": "8.0.0",
    "pixelmatch": "^5.3.0",
    "postcss": "^8.4.31",
    "postcss-aspect-ratio": "^1.0.2",
    "postcss-clean": "1.2.0",
    "postcss-import": "15.1.0",
    "postcss-preset-env": "9.2.0",
    "postcss-url": "10.1.3",
    "postinstall-prepare": "^2.0.0",
    "prettier": "2.7.1",
    "purgecss": "5.0.0",
    "ramda": "^0.28.0",
    "react": "^16.14.0",
    "react-dom": "^16.14.0",
    "replace-in-file": "^6.3.5",
    "request": "^2.88.2",
    "rework": "^1.0.1",
    "rework-mutate-selectors": "^2.0.0",
    "rework-pure-grids": "^2.0.1",
    "rimraf": "^3.0.2",
    "rollup": "^3.19.1",
    "rollup-plugin-copy": "^3.4.0",
    "rollup-plugin-delete": "^2.0.0",
    "rollup-plugin-shell": "^1.0.9",
    "rollup-plugin-styles": "^4.0.0",
    "scss-bundle": "^3.1.2",
    "sort-package-json": "^2.6.0",
    "storybook-design-token": "^3.0.0-beta.6",
    "style-dictionary": "^3.8.0",
    "stylelint": "^15.10.3",
    "stylelint-config-idiomatic-order": "^8.1.0",
    "stylelint-config-standard-scss": "^11.0.0",
    "stylelint-declaration-strict-value": "^1.10.4",
    "stylelint-scss": "^5.2.1",
    "tailwindcss": "^3.3.3",
    "ts-morph": "^20.0.0",
    "typescript": "5.2",
    "uglify-js": "^3.16.1",
    "verdaccio": "^5.0.4",
    "webpack": "5.88.2",
    "yargs": "^17.5.1"
  },
  "engines": {
    "node": ">= 8.11.1",
    "npm": ">= 5.0.0"
  }
}
```


### Anything else?

_No response_