0Pricing
Sveltejs Academy · Lesson

onDestroy: Cleanup

Run teardown logic such as clearing timers or unsubscribing when the component is removed.

Why onDestroy

onDestroy runs when a component is removed from the DOM. Use it to tear down listeners, timers, or subscriptions.

Import and Call

Import from svelte and pass a callback.

<script>
  import { onDestroy } from "svelte";
  onDestroy(() => console.log("bye"));
</script>

All lessons in this course

  1. onMount: Running After Render
  2. onDestroy: Cleanup
  3. beforeUpdate and afterUpdate
  4. tick(): Waiting for DOM Updates
← Back to Sveltejs Academy