angular-destroyref

Migrate Angular subscriptions from ngOnDestroy and Subject patterns to DestroyRef and takeUntilDestroyed.

6|1|Updated Feb 18, 2026
One-click install
npx skills add https://github.com/oguzhan18/angular-ecosystem-skills --skill angular-destroyref
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: angular-destroyref
Source: https://github.com/oguzhan18/angular-ecosystem-skills/tree/main/skills/angular-destroyref
Command: npx skills add https://github.com/oguzhan18/angular-ecosystem-skills --skill angular-destroyref

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

## What problem does it solve? Angular components and services often leak subscriptions and resources when cleanup is handled manually with Subjects or ngOnDestroy, leading to memory leaks and unpredictable behavior. This Skill provides clear patterns and examples to adopt DestroyRef and takeUntilDestroyed for deterministic teardown and safer lifecycle management.

## Core Features & Use Cases

  • Deterministic Teardown: Shows how to use takeUntilDestroyed with injected DestroyRef to automatically unsubscribe observables in components and services.
  • Migration Guidance: Describes replacing manual Subject-based cleanup or ngOnDestroy logic with DestroyRef-based patterns.
  • Practical Examples: Includes component and service snippets that demonstrate injection of DestroyRef and integration with RxJS pipelines, useful for refactoring and code reviews.

### Quick Start Ask the skill to analyze a component and suggest a migration that replaces manual ngOnDestroy/Subject teardown with DestroyRef and takeUntilDestroyed.

Frequently Asked Questions about angular-destroyref

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I prevent memory leaks in Angular components and services?

To prevent memory leaks in Angular components and services, use the DestroyRef API and takeUntilDestroyed operator to automatically unsubscribe observables and ensure deterministic teardown of resources during the component lifecycle.

How do I migrate from ngOnDestroy and Subject-based cleanup to takeUntilDestroyed?

Migrating from ngOnDestroy and Subject-based cleanup to takeUntilDestroyed involves injecting DestroyRef into your component or service, then applying the takeUntilDestroyed operator within your RxJS pipeline to handle automatic subscription teardown without manual lifecycle hooks.

What is the best way to automatically unsubscribe RxJS observables in Angular 16+?

The best way to automatically unsubscribe RxJS observables in Angular 16+ is using the DestroyRef API combined with the takeUntilDestroyed operator from @angular/core/rxjs-interop, which provides deterministic teardown for subscription lifecycles.

Does DestroyRef work for cleaning up resources in Angular services, or only components?

DestroyRef works for cleaning up resources in both Angular components and services, allowing you to inject it wherever needed and manage observable teardown and resource destruction without relying solely on the component ngOnDestroy lifecycle hook.

Why does my manual Subject cleanup still cause memory leaks in Angular?

Manual Subject cleanup causes memory leaks in Angular when teardown logic is incomplete or improperly triggered, making the deterministic DestroyRef pattern a safer approach for managing subscription lifecycles and preventing unpredictable behavior.