This update finally fixes Angular's messy service persistence by automating cleanup during route deactivation. It is a necessary evolution that simplifies memory management and removes the need for manual lifecycle hacks.
Deep Dive
Prerequisite Knowledge
- No data available.
Where to go next
- No data available.
Deep Dive
How to PROPERLY Clean Up Angular Services on Route DeactivationAdded:
Hi.
I would like to challenge you with a Quick question that... a lot of Angular developers fail at interviews... the question is very simple, and it goes like: will the data pulling service be destroyed when the user navigates away from the user's route?
Well, despite it being so natural to think that the service instance will be destroyed, that is not true.
By default, the injector created for that particular route is retained in memory forever.
The great news is that In modern Angular, we can change this behavior π and in this video I will show you how to do that and some real life use cases where it might be applicable.
So let me quickly walk you through the app we are working with.
So here we have a user's route, and when a user navigates to it, a data polling service starts a polling session in the background, checking the server for new data.
And you might be curious why I initialize the polling session inside the environment Initializer, instead of doing it in the user dashboard component, for example.
if you would like to learn more about Environment Initializer and why I use it here, I have a dedicated video where I covered all this things, so you can check this video out later if you want.
So now that you know the setup, let me show you what the actual problem looks like.
So we start polling session when user navigates to users' route.
Now imagine the user navigates away what happens?
Right?
The polling keeps running and the service instance is still sitting in memory.
And it will stay there for the entire lifetime of the app.
This is not necessarily a problem.
Sometimes it is the desired behavior, but otherwise it might cause unnecessary load on your server.
So how did people deal with this issue before Angular 21.1?
there were a few options and honestly, none of them are great.
The first option would be to stop polling inside the root component in geo destroy, lifecycle hook, but that couples the service, uh, or polling lifecycle with a specific component, and that is quite fragile and not scalable.
The second solution would be using that can deactivate router guard, but this idea is even worse because.router guards are designed for the navigation control and not for performing something like cleanup logic or things like that.
The third option would be to build a separate service that would listen to the router events and detect when a specific route gets deactivated.
This might work, but in my opinion, it requires a lot of code for something that should be simple, if not default.
And on top of that, none of those solutions are actually destroy the service instance.
So the injector stays in memory either way.
The cool thing is that in Angular 21.1, a new router feature was introduced that solves exactly this problem, and it is called without cleanup, injectors.
Now let me quickly show you how it works.
To enable it, you have to go to your app config ts file.
Then you have to import this feature function.
And added to the router feature list when you configure your angular router, and that's it, one line of configuration.
So now whenever the user navigates away from the user's route, the corresponding route injector is destroyed and the polling service instance is destroyed along with it.
And this is exactly what we need.
Right now, however, you can see that the polling is still ticking, but this is because the RXGS subscription in the polling service is kind of leaking, so... to fix this, we have to go to the service and stop our sub subscription explicitly in the ngOnDestroy Lifecycle hook.
So now, once we save this change.
Our polling stops immediately when user navigates away from the user's page, and then polling starts again when we come back to it.
That's cool.
But what could go wrong here?
Well, it might happen that you want to stop a service subscription when a service gets destroyed, but you have no access to the service, uh, source code, for example, the service is coming from third party library.
What to do in this case?
The solution is pretty simple.
Angular has a special injectable class called Destroy Rev, which allows you to define certain cleanup logic that runs when the corresponding injector gets destroyed.
This class can be injected almost everywhere, including inside the environment.
Initializer callback since it is being executed in the injection context.
So once you have it injected, you can define there on destroy.
Call back and stop the app polling session by calling the corresponding stop method.
So now if you have the auto cleanup injectors router feature enabled for your application and the user leaves the user's route, for example then the on Destroy lifecycle hook we just registered will be invoked and the polling subscription is stopped.
And we could achieve that without modifying the source code of the polling service itself.
But is there anything else we should care about?
Let's figure it out.
If you decided to enable the auto cleanup injectors feature in your angular application, please keep in mind one edge case where it might not work as expected.
So if your router is configured with a custom route to use strategy, you might need additional configuration to make it work.
The first thing you have to do is to check if your custom reuse strategy class extends the base route reuse strategy.
And if it does and you want the route injector to be destroyed after the activation, then you should do nothing.
It will work as expected automatically but otherwise, your custom reuse strategy class must implement a new method called shouldDestroyInjector, which should return true or false depending on the behavior you want to achieve.
So in this way, your reuse strategy will work correctly with the new auto cleanup injector router feature.
All right I,think we covered pretty much everything so let's do a quick recap.
So using withAutoCleanupInjectorsinjector router feature, we can implement a clean data polling solution.
It starts when user enter a particular route and stops when the user leaves it.
The polling data service is decoupled from any component lifecycle, which makes this solution flexible, scalable, and predictable.
But this is more than just a polling solution!
If you think about it, combined with environment Initializer, we now have something like onInit and onDestroy lifecycle hooks for the route injector.
And this is really cool because it opens doors for a lot of other interesting architectural decisions and solutions.
So enable this feature if you want to clean up memory by destroying service instances from the inactive route.
If you want to perform cleanup logic when the particular route is deactivated Or use it in combination with the DestroyRef to achieve more flexibility.
But before using it, please keep in mind the following.
This feature is available starting from Angular 21.1.
This is still experimental feature, so the API and its behavior might change in the future.
And also it can be a breaking change for your application, especially if you use route reuse strategies, so make sure that you test things properly after you enable this feature.
and talking about testing.
I cannot go without mentioning my friends from test Sprite.
I.
? It is an AI testing agent that I have already mentioned before and it fits really well if you are adopting AI tools in your workflow.
The setup of test Sprite is extremely simple.
You just have to create a free account on their website and connect the MCP server to tools like CLO Code or coor, and then you simply ask it to test your application.
It'll analyze your code base and ask for a product requirement document, which is a simple MD file that describes what your application is doing in order to understand what to test, and based on that, it generates a test plan, it creates tests, executes them, and gives you a final report.
What is cool is that now you can actually observe the whole testing process in real time, including live status updates, detailed logs, recordings of how exactly your application or component has been tested.
And maintaining tests over time got easier as well since you can edit your test directly using the interactive UI tools.
If it looks exciting and you would like to play with test Sprite more, you can get one month free on their starter plan following the link in the description.
Alright, my friends, if you want to continue sharpen your angular knowledge about related topics, there are few videos on my YouTube channel I would recommend watching Next.
The first video is obviously about environment initializer that I already mentioned in the beginning of this video.
You can learn more about, root level injectors in my another dedicated video and also I think I mentioned memory leaks, and, about this topic, I have even two great videos where I show how to debug them and how to fix them.
So if you want, you can check them out right now.
and if you find this video useful and you would like to see more videos like this, the best way you can support my channel is to subscribe to it and share this video with someone on your team who works with Angular and want to improve the skills.
It really helps and I appreciate it so, so much.
Otherwise, thank you so much for watching this video until the end.
I wish you productive week ahead.
Stay safe and see you in the next video.
Related Videos
Agentforce NOW AMA: Build with React and Salesforce Multi-Framework
SalesforceDevs
490 viewsβ’2026-05-28
How agent o11y differs from traditional o11y β Phil Hetzel, Braintrust
aiDotEngineer
450 viewsβ’2026-05-28
WEB TECHNOLOGIES UNIT-2 | Degree 4th sem BCOM Computers web technologies unit-2 full explanationπ―β
LearnwithSahera
1K viewsβ’2026-05-29
More tests are always better? How to use AI to identify tests that bring little value
Alliance4Qualification
335 viewsβ’2026-05-29
Search Algorithms Explained in 60 Seconds! π€π¨
samarthtuliofficial
218 viewsβ’2026-06-01
People of Game of Thrones using JavaScript DOM
AltCampus
296 viewsβ’2026-05-30
Introduction to Problem Solving Part - 1 | Lecture 1 | Intermediate DSA
ascensionix
107 viewsβ’2026-05-29
So What's Odin Lang Even Good For
TechOverTea
131 viewsβ’2026-06-01











