0Pricing
Mojo Academy · Lesson

Destruction and __del__

Clean up when a value's life ends.

Destruction and __del__ is a free Mojo Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Mojo Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Every Value Has an End

When a value is no longer needed, its life ends and Mojo destroys it. This is your chance to clean up what it owned. 🧹

The Destructor Method

Mojo calls a special method named __del__ to destroy a value. It runs automatically when the value goes out of scope.

fn __del__(owned self):
    pass

Deterministic Timing

Mojo destroys values at a predictable point, as soon as they are last used. There is no waiting on a garbage collector.

Last Use, Not Block End

Mojo runs __del__ right after a value is last used, often before the block closes. This frees resources as early as possible.

Free What You Own

If your type holds a heap buffer, __del__ is where you release it. Doing so prevents memory from leaking over time.

You Own the Cleanup

Inside __del__ the value is owned, so you can safely tear down its resources. After this, the value no longer exists.

Often Synthesized

For simple types you write nothing. Mojo synthesizes a destructor that cleans up each field in turn, so leaks are avoided.

When to Customize It

Write your own __del__ when you manage a raw resource, like an allocation or file handle, that needs explicit release.

Runs Once, Exactly

Each value is destroyed exactly once. Moves transfer ownership so only the final holder runs __del__, avoiding double frees.

No Use After Death

Once __del__ completes, Mojo guarantees the value is gone. Its ownership rules stop you from touching freed data.

Safety Without a GC

Deterministic destruction gives you C-like control with built-in safety. Cleanup is timely, predictable, and automatic.

Quick Check

Lets check destruction.

Recap

__del__ runs when a value’s life ends, deterministically and exactly once, letting you clean up safely without a GC. 🎯

Frequently asked questions

Is the “Destruction and __del__” lesson free?

Yes — the full text of “Destruction and __del__” is free to read here on the web, and the Mojo Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Mojo Academy course, upgrade to CoddyKit PRO.

What will I learn in “Destruction and __del__”?

Clean up when a value's life ends. You practise Mojo Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Mojo Academy?

No prior experience is required. Mojo Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Destruction and __del__” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Mojo Academy lesson?

Yes. Every Mojo Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Copy vs Move Semantics
  2. The __copyinit__ Method
  3. The __moveinit__ Method
  4. Destruction and __del__
← Back to Mojo Academy