Delete a component in Angular

Delete a component in Angular

To delete a component in Angular, you need to:

  1. Remove the import line reference from Angular app.module.ts file.

  2. Remove the component declaration from @NgModule declaration array in app.module.ts file.

  3. Manually delete the component folder from Angular project.

  4. Delete all the references of component manually from the Angular project.

Note: Angular CLI does not currently support a command to delete a component, so you need to do it manually.

Here are the steps in more detail:

  1. Open the src/app/app.module.ts file.
  2. Find the import line for the component you want to delete.
  3. Delete the import line.
  4. Find the component declaration in the @NgModule declaration array.
  5. Delete the component declaration.
  6. Save the file.
  7. Open the folder containing the component you want to delete.
  8. Delete the folder.
  9. Search the project for any other references to the component and delete them.

Once you have completed these steps, the component will be removed from your Angular project.

Here is an example of how to delete a component called MyComponent:

// Remove the import line reference.
import { MyComponent } from './my-component/my-component.component';

// Remove the component declaration from @NgModule declaration array.
@NgModule({
  imports: [...],
  declarations: [MyComponent],
  providers: [...],
  bootstrap: [AppComponent]
})
export class AppModule {}
// Delete the component folder.
src/app/my-component/my-component.component.html
src/app/my-component/my-component.component.css
src/app/my-component/my-component.component.ts
// Delete any other references to the component in the project.

Once you have completed these steps, the MyComponent component will be removed from your Angular project.