[angular] What is the best way to delete a component with CLI

I tried using "ng destroy component foo" and it tells me "The destroy command is not supported by Angular-CLI"

How do we properly delete components with Angular CLI?

This question is related to angular angular-cli

The answer is


destroy or something similar may come to the CLI, but it is not a primary focus at this time. So you will need to do this manually.

Delete the component directory (assuming you didn't use --flat) and then remove it from the NgModule in which it is declared.

If you are unsure of what to do, I suggest you have a "clean" app meaning no current git changes. Then generate a component and see what is changed in the repo so you can backtrack from there what you will need to do to delete a component.

Update

If you're just experimenting about what you want to generate, you can use the --dry-run flag to not produce any files on disk, just see the updated file list.


I needed to delete an Angular 6 directive whose spec file was erroneous. Even after deleting the offending files, removing all references to it and rebuilding the app, TS was still reporting the same error. What worked for me was restarting Visual Studio - this cleared the error and all traces of the old unwanted directive.


First of all, remove component folder, which you have to delete and then remove its entries which you have made in "ts" files.


Answer for Angular 2+

Remove component from imports and declaration array of app.modules.ts.

Second check its reference is added in other module, if yes then remove it and

finally delete that component Manually from app and you are done.

Or you can do it in reverse order also.


Using Visual Studio Code, delete the component folder and see in the Project Explorer(left hand side) the files that colors Red that means the files are affected and produced errors. Open each files and remove the code that uses the component.


If you looking for some command in CLI, Then ans is NO for now. But you can do manually by deleting the component folder and all the references.


  1. Delete the folder containing this component.
  2. In the app.module.ts remove the import statement for this component and remove its name from the declaration section of @NgModule
  3. Remove the line with the export statement for this component from index.ts.

Currently Angular CLI doesn't support an option to remove the component, you need to do it manually.

  1. Remove import references for every component from app.module
  2. Delete component folders.
  3. You also need to remove the component declaration from @NgModule declaration array in app.module.ts file

Angular 10 now I am using. After manually deleting all files and references then I re-run "ng serve", it worked.


I wrote a bash script that should automate the process written by Yakov Fain below. It can be called like ./removeComponent myComponentName This has only been tested with Angular 6

#!/bin/bash
if [ "$#" -ne 1 ]; then
    echo "Input a component to delete"
    exit 1
fi

# finds folder with component name and deletes
find . -type d -name $1 | xargs rm -rf

# removes lines referencing the component from app.module.ts
grep -v $1 app.module.ts > temp
mv temp app.module.ts

componentName=$1
componentName+="Component"

grep -v -i $componentName app.module.ts > temp
mv temp app.module.ts

Since it is not yet supported using angular CLI

so here is the possible way, before that please observe what happens when you create a component/service using CLI (ex. ng g c demoComponent).

  1. It creates a separate folder named demoComponent (ng g c demoComponent).
  2. It generate HTML,CSS,ts and a spec file dedicated to demoComponent.
  3. Also, It adds dependency inside app.module.ts file to add that component to your project.

so do it in reverse order

  1. Remove Dependency from app.module.ts
  2. Delete that component folder.

when removing the dependency you have to do two things.

  1. Remove the import line reference from app.module.ts file.
  2. Remove the component declaration from @NgModule declaration array in app.module.ts.

From app.module.ts:

  • erase import line for the specific component;
  • erase its declaration from @NgModule;

Then delete the folder of the component you want to delete and its included files (.component.ts, .component.html, .component.css and .component.spec.ts).

Done.

-

I read people saying about erasing it from main.ts. You were not supposed to import it from there in the first place as it already imports AppModule, and AppModule is the one importing all the components you created.


I am not sure if it is the best way, but it worked for me.

  • First, I deleted the component folder.
  • Then, I cleared app.module.ts, app.component.ts & app.component.html of the imports and declarations related to the component I wanted to delete.
  • Similarly, I cleared main.ts.

I just saved and refreshed the app and it worked.


This is not supported by Angular CLI and they are in no mood to include it any time soon.

Here is the link to the actual created issue - https://github.com/angular/angular-cli/issues/1776

And a screenshot of the solution from the officials - enter image description here


Examples related to angular

error NG6002: Appears in the NgModule.imports of AppModule, but could not be resolved to an NgModule class error TS1086: An accessor cannot be declared in an ambient context in Angular 9 TS1086: An accessor cannot be declared in ambient context @angular/material/index.d.ts' is not a module Why powershell does not run Angular commands? error: This is probably not a problem with npm. There is likely additional logging output above Angular @ViewChild() error: Expected 2 arguments, but got 1 Schema validation failed with the following errors: Data path ".builders['app-shell']" should have required property 'class' Access blocked by CORS policy: Response to preflight request doesn't pass access control check origin 'http://localhost:4200' has been blocked by CORS policy in Angular7

Examples related to angular-cli

Why powershell does not run Angular commands? ERROR in The Angular Compiler requires TypeScript >=3.1.1 and <3.2.0 but 3.2.1 was found instead Angular CLI Error: The serve command requires to be run in an Angular project, but a project definition could not be found Could not find module "@angular-devkit/build-angular" How to remove package using Angular CLI? How to set environment via `ng serve` in Angular 6 Error: Local workspace file ('angular.json') could not be found How do I deal with installing peer dependencies in Angular CLI? How to iterate using ngFor loop Map containing key as string and values as map iteration how to format date in Component of angular 5