To remove unused API-client code from an Angular app, build with production optimization, keep the client’s modules and imports statically analyzable, and verify the emitted production bundle. Tree-shaking can remove unreferenced modules when the package structure and side effects allow it; it does not guarantee that unused methods disappear from a service class the app still uses.
1. Confirm which Angular build you are optimizing
Start with the build target in angular.json. Angular applications and libraries use different builders, so do not apply application-build assumptions to a library build. Angular documents @angular/build:application as its application builder, which uses esbuild for new CLI projects, and @angular/build:ng-packagr as its library builder.
For an application, use its production configuration or otherwise enable the corresponding optimization setting. Angular’s application optimization includes script and style minification, tree-shaking, dead-code elimination, critical CSS, and font inlining. See Angular build options for the build configuration and optimization settings.
2. Give the bundler clean import boundaries
Use ordinary ESM imports from the smallest entrypoint the client supports. If you maintain the client, split genuinely unrelated capability groups into separate modules or package entrypoints, so an application can import only the services it needs. Avoid a broad barrel import when that barrel eagerly pulls in services or operations through value references or side-effectful code.
#1 Best Overall
Angular Package Format describes primary and secondary entrypoints as distinct import specifiers and explains how top-level side effects can limit tree-shaking. Its guidance recommends declaring "sideEffects": false for packages that are genuinely free of top-level side effects. Treat that as a factual package claim, not a performance switch: if a module relies on top-level registration or other required work, an inaccurate declaration can cause that behavior to be dropped. See Angular Package Format.
3. Inspect how the generated client is organized
If you use OpenAPI Generator’s typescript-angular generator, inspect the generated service and model files, public barrel exports, imports between services, and package metadata. The generator documents a providedIn option with root as the default and none, any, and platform as other choices. That option controls Angular provider scope; it is not evidence that individual endpoint methods will be removed from a service that remains in use.
Rank #2
Setting providedIn to none may require the application to provide the service manually, so make the choice for its injector and lifecycle behavior as well as bundle considerations. Check the installed generator’s documentation and output: the published compatibility range and defaults can change across releases. See the OpenAPI Generator TypeScript-Angular documentation.
When services are grouped by API or tag
If the generated client has separate services for API groups or tags, importing only the services the app uses can give the bundler a useful module boundary. This works only if exports, imports, and side effects preserve that separation. The generator documentation does not promise one independently tree-shakable file per endpoint, so do not equate a generated service boundary with endpoint-level elimination.
Free tools Windows power users keep installed
One-click scans. No signup required.
Rank #3
4. Check whether dependency injection retains optional code
Angular’s library-design guidance recommends tree-shakable providers: services should declare their own providers rather than relying on providers declared in an NgModule or component. Its lightweight injection-token guidance also notes that runtime dependency-injection references can keep an otherwise unused service or component in the bundle.
If an optional API capability is injected into a widely used service or component, consider whether a small abstract token can separate the widely used code from the concrete implementation, with the implementation provided later where appropriate. This pattern is useful for some library designs; it is not a requirement to add token abstractions to every generated client. See Creating libraries and Lightweight injection tokens.
Rank #4
5. Verify the result in a controlled production build
Source-level expectations are not a substitute for checking what your app emits. Compare two production builds made with the same toolchain and configuration: one with the target service or import present, and one with it removed. Inspect emitted chunks and, if your workflow supports them, bundle details or source maps. Keep the test on the production-optimized application builder rather than relying on a development build.
- Record the Angular version, actual build target and builder, OpenAPI Generator version, and import pattern.
- Build the application with the service or import included, using the production configuration.
- Remove that service or import without changing other build settings, then build again.
- Compare the emitted chunks and inspect the output to see whether the client code was removed.
There is no documented, generally applicable percentage of bundle-size savings for removing endpoints from an Angular API client. Report what your own optimized builds show, and tie the conclusion to the versions, builder, client structure, and import pattern you measured.
Do these 3 things before closing this tab:
1Scan for outdated or missing drivers - takes under a minute2Repair Windows errors before they cause bigger problems3Fix the driver behind crashes, sound loss and screen glitchesChoose the right level of granularity
| Approach | What it can help remove | Trade-off or limit |
|---|---|---|
| One large API service | Potentially the service if nothing in the app references it and package structure permits removal | Do not assume unused methods disappear independently when the service is retained. |
| Separate services by API or tag | Unreferenced service modules, if imports, exports, and side effects leave them removable | The generator documentation does not guarantee a separate module for every endpoint. |
| Separate modules or package entrypoints for capabilities | Unimported capability modules at clear ESM boundaries | More structure to maintain; top-level side effects or broad value references can defeat removal. |
Change the generator’s providedIn setting |
Provider configuration and injector behavior | Does not by itself establish endpoint-level elimination; none may require manual provision. |
For library authors, Angular’s concise rule is: “Services should declare their own providers, rather than declaring providers in the NgModule or a component. Declaring a provider makes that service tree-shakable.” Apply it alongside modular ESM exports and truthful side-effect metadata, then confirm the actual production output.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




