JavaScript Worker terminate() Method

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Baseline Widely available
Instance method

What You’ll Learn

The terminate() method of Worker immediately stops the worker from the main thread. It does not let the worker finish or clean up. Learn when to use it, how it differs from self.close(), and how to free blob URLs—with five examples and try-it labs.

01

Kind

Instance method

02

Returns

undefined

03

Params

None

04

Stops

Immediately

05

vs

self.close()

06

Status

Baseline widely

Introduction

A dedicated worker started with new Worker() keeps running until you stop it or the page goes away. When the job is done—or you must cancel heavy work—call worker.terminate() from the main thread.

MDN is clear: the worker is stopped at once. There is no chance to flush files, send a final message, or tidy up. Prefer a cooperative “please stop” message plus self.close() inside the worker when you need a graceful exit.

💡
Beginner tip

Think of terminate() as the emergency stop button on the main thread. Think of self.close() as the worker choosing to shut itself down after finishing.

Understanding Worker.terminate()

An instance method that immediately terminates the dedicated Worker.

  • No parameters — call worker.terminate().
  • Return value — none (undefined).
  • Immediate — no opportunity for the worker to finish operations.
  • One-shot — that Worker object cannot be restarted; create a new one.
  • Baseline Widely available on MDN (since July 2015); available in Web Workers except Service Workers.

📝 Syntax

JavaScript
terminate()

Parameters

None.

Return value

None (undefined).

Typical pattern

JavaScript
const myWorker = new Worker("worker.js");
// … use postMessage / onmessage …
myWorker.terminate();

⚡ Quick Reference

GoalCode / note
Hard stopworker.terminate()
Soft stopMessage the worker; it calls self.close()
Free blobURL.revokeObjectURL(url) after terminate
Restartnew Worker(...) again
MDN statusBaseline Widely available (since July 2015)

🔍 At a Glance

Four facts to remember about terminate().

Stops
now

No cleanup

Caller
main

Not the worker

Reuse
no

Create new Worker

Baseline
widely

Since Jul 2015

Examples Gallery

Examples follow MDN Worker: terminate(). Labs use blob workers so they run in one HTML file.

📚 Getting Started

Create a worker and stop it from the main thread.

Example 1 — Create Then Terminate (MDN Idea)

Start a worker and stop it right away.

JavaScript
const myWorker = new Worker(url);
myWorker.terminate();
console.log("Worker terminated");
Try It Yourself

How It Works

Matches MDN’s minimal snippet: construct, then terminate().

Example 2 — Terminate After a Reply + Revoke Blob URL

Use the worker once, then free resources.

JavaScript
worker.onmessage = (e) => {
  console.log(e.data);
  worker.terminate();
  URL.revokeObjectURL(url);
};
worker.postMessage("ping");
Try It Yourself

How It Works

Common demo pattern: one round-trip, then hard stop and revoke the blob.

📈 Cancel, Silence & Soft Exit

What happens after terminate, canceling work, and close().

Example 3 — No Reply After Terminate

Messages sent after terminate do not get a normal worker answer.

JavaScript
worker.terminate();
worker.postMessage("too late");
// onmessage will not fire for a healthy reply
Try It Yourself

How It Works

Clear pending UI state when you terminate so you do not wait forever.

Example 4 — Cancel Long Work

Stop a looping worker when the user clicks Cancel.

JavaScript
// worker spins counting until terminated
cancelBtn.onclick = () => {
  worker.terminate();
  out.textContent = "Canceled";
};
Try It Yourself

How It Works

Hard cancel is exactly what terminate is for when cooperative stop is too slow.

Example 5 — Prefer self.close() for Graceful Exit

Worker finishes a job, then closes itself (MDN notes this alternative).

JavaScript
// inside worker:
self.onmessage = (e) => {
  const result = doWork(e.data);
  self.postMessage(result);
  self.close(); // worker stops itself after sending
};
Try It Yourself

How It Works

Use close when the worker can finish cleanly; use terminate when main must force-stop.

🚀 Common Use Cases

  • Cancel a background job when the user navigates away or presses Cancel.
  • Tear down one-shot demo workers after a single reply.
  • Free CPU when a page no longer needs a pool worker.
  • Recover from a stuck worker by terminating and creating a new one.
  • Pair with revokeObjectURL for blob-based workers.

🔧 How It Works

1

Worker is running

It may be mid-message or mid-loop on its thread.

Alive
2

Main calls terminate()

The browser stops the worker immediately.

Stop
3

No graceful finish

Pending worker work is abandoned; no cleanup hook.

Hard
4

Clean up on main

Drop references; revoke blob URLs; create a new Worker if needed.

📝 Notes

  • MDN: Baseline Widely available (since July 2015) — no Deprecated / Experimental / Non-standard banner.
  • Available in Web Workers except Service Workers.
  • DedicatedWorkers can also stop via DedicatedWorkerGlobalScope.close().
  • SharedWorkers have SharedWorkerGlobalScope.close() (different API surface).
  • Related learning: postMessage(), Worker(), JavaScript hub.

Universal Browser Support

Worker.terminate() is marked Baseline Widely available on MDN (since July 2015). Logos use the shared browser-image-sprite.png sprite from this project. It is available in Web Workers except Service Workers.

Baseline · Widely available

Worker.terminate()

Immediately terminates a dedicated Worker from the main thread—no chance for the worker to finish.

Universal Widely available
Google Chrome Full support · Desktop & Mobile
Full support
Mozilla Firefox Full support · Desktop & Mobile
Full support
Apple Safari Full support · macOS & iOS
Full support
Microsoft Edge Full support · Chromium
Full support
Opera Full support · Modern versions
Full support
Internet Explorer Supported with workers in IE10+ (prefer modern browsers)
Legacy
Worker.terminate() Excellent

Bottom line: Use terminate() for hard cancels from main; prefer self.close() when the worker can exit cleanly after finishing.

Conclusion

Worker.terminate() is the main-thread hard stop for a dedicated worker. Use it to cancel or tear down; use self.close() when the worker should finish gracefully. Always revoke blob URLs you created.

Continue with message, postMessage(), or the JavaScript hub.

💡 Best Practices

✅ Do

  • Terminate when canceling user-facing work
  • Revoke blob URLs after stopping blob workers
  • Prefer close() when the worker can exit cleanly
  • Create a new Worker() if you need to restart
  • Clear UI “waiting” state on terminate

❌ Don’t

  • Expect cleanup code in the worker after terminate
  • Reuse a terminated Worker instance
  • Leave blob URLs unreclaimed in long sessions
  • Assume postMessage will still get replies
  • Use terminate as the only strategy for every exit

Key Takeaways

Knowledge Unlocked

Five things to remember about terminate()

The emergency stop from the main thread.

5
Core concepts
🚨02

No cleanup

in worker

Risk
🔁03

vs close

soft exit

Compare
🗑04

Revoke

blob URLs

Cleanup
🎯05

Baseline

since Jul 2015

Status

❓ Frequently Asked Questions

It immediately stops a dedicated Worker from the main thread. The worker does not get a chance to finish current work or run cleanup—it is halted at once.
No. MDN marks Worker.terminate() as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard. It is available in Web Workers except Service Workers.
No graceful shutdown from terminate(). If you need the worker to finish or clean up, send a message and let it call self.close() (or SharedWorkerGlobalScope.close() for shared workers) instead.
No. After terminate(), that Worker instance is done. Create a new Worker() if you need another background script.
Yes when you created the worker from URL.createObjectURL(blob). Call URL.revokeObjectURL(url) after terminate() so the blob can be garbage-collected.
postMessage after terminate will not get a normal worker reply. Treat the worker as gone and discard pending expectations.
Did you know?

MDN notes that dedicated and shared workers can also stop from inside via close() on their global scope. That path lets the worker finish sending a final result before exiting—something terminate() never allows.

Next: message event

Learn how the main thread receives replies from a dedicated worker.

message event →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful