JavaScript MediaStream removeTrack() Method

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

What You’ll Learn

The removeTrack() method of MediaStream removes a MediaStreamTrack from a stream. Learn the MDN pattern that empties a stream, removing only the camera while keeping the mic, pairing with addTrack(), and the difference between removeTrack() and track.stop()—with five examples and try-it labs.

01

Kind

Instance method

02

Parameter

track

03

Returns

undefined

04

Effect

detach track

05

Use case

MDN empty

06

Status

Baseline widely

Introduction

A MediaStream holds references to MediaStreamTrack objects. When you compose streams manually with addTrack() or copy tracks into a new stream, you may later need to detach a track without destroying it. removeTrack(track) removes that reference from the stream’s track set.

MDN’s example captures audio and video with getUserMedia, copies tracks into a new MediaStream, then removes both tracks—leaving getTracks() as an empty array. Important: removeTrack() does not call stop() on the track; the hardware may still be active until you stop it separately.

💡
Beginner tip

To fully release a camera or mic, call track.stop() after (or instead of) removing it from the stream.

Understanding MediaStream.removeTrack()

An instance method that removes one track from this stream’s track set.

  • Parameterstrack: a MediaStreamTrack currently in the stream.
  • Return valueundefined (none).
  • Effect — track is no longer listed by getTracks() on this stream.
  • Not the same as stop — the track object may still be live after removal.
  • Pair withaddTrack(), getTracks(), active.
  • Baseline Widely available on MDN (since September 2017).

📝 Syntax

JavaScript
removeTrack(track)

Parameters

track — A MediaStreamTrack to remove from this stream.

Return value

None (undefined).

Typical pattern (MDN idea)

JavaScript
let newStream = new MediaStream(initialStream.getTracks());

const videoTrack = newStream.getVideoTracks()[0];
const audioTrack = newStream.getAudioTracks()[0];

newStream.removeTrack(videoTrack);
newStream.removeTrack(audioTrack);

// Stream will be empty
console.log(newStream.getTracks());

⚡ Quick Reference

GoalCode / note
Remove one trackstream.removeTrack(track)
Remove video onlystream.removeTrack(stream.getVideoTracks()[0])
Check what remainsstream.getTracks().length
Detach and releaseremoveTrack(t); t.stop()
Empty stream (MDN)Remove every track—getTracks() is []
MDN statusBaseline Widely available (since September 2017)

🔍 At a Glance

Four facts to remember about MediaStream.removeTrack().

Param
track

MediaStreamTrack

Returns
undefined

Nothing

Effect
detach

From stream

Baseline
widely

Since Sep 2017

Examples Gallery

Examples follow MDN MediaStream: removeTrack(). Demos use getUserMedia (secure context + permission).

📚 Getting Started

MDN pattern: remove all tracks until the stream is empty.

Example 1 — MDN: Remove Audio and Video (Empty Stream)

Copy tracks to a new stream, then remove both—getTracks() is [].

JavaScript
const initialStream = await navigator.mediaDevices.getUserMedia({
  video: true,
  audio: true,
});

const newStream = new MediaStream(initialStream.getTracks());

const videoTrack = newStream.getVideoTracks()[0];
const audioTrack = newStream.getAudioTracks()[0];

newStream.removeTrack(videoTrack);
newStream.removeTrack(audioTrack);

console.log(newStream.getTracks()); // []
Try It Yourself

How It Works

MDN: after both removals, the stream has no tracks. Remember to stop() hardware tracks when done.

Example 2 — Remove Video, Keep Audio

Detach only the camera track from a composed stream.

JavaScript
navigator.mediaDevices
  .getUserMedia({ audio: true, video: true })
  .then((stream) => {
    const videoTrack = stream.getVideoTracks()[0];
    stream.removeTrack(videoTrack);

    console.log(
      stream.getVideoTracks().length,
      stream.getAudioTracks().length
    );
  });
Try It Yourself

How It Works

Audio remains in the stream; video is detached. Call videoTrack.stop() to turn off the camera.

📈 Composition & Cleanup

Pair with addTrack and distinguish from stop().

Example 3 — addTrack() Then removeTrack()

Build a stream incrementally, then remove a track.

JavaScript
navigator.mediaDevices
  .getUserMedia({ audio: true, video: true })
  .then((source) => {
    const composed = new MediaStream();
    const audio = source.getAudioTracks()[0];
    const video = source.getVideoTracks()[0];

    composed.addTrack(audio);
    composed.addTrack(video);
    composed.removeTrack(video);

    console.log(composed.getTracks().length, composed.getAudioTracks().length);
  });
Try It Yourself

How It Works

removeTrack() is the inverse of addTrack() for manual stream building.

Example 4 — removeTrack() vs track.stop()

Removal detaches from the stream; stop() ends the track.

JavaScript
navigator.mediaDevices
  .getUserMedia({ video: true })
  .then((stream) => {
    const track = stream.getVideoTracks()[0];
    stream.removeTrack(track);

    // Track may still be "live" until stop()
    console.log(track.readyState);
    track.stop();
    console.log(track.readyState);
  });
Try It Yourself

How It Works

Use removeTrack() to update stream composition; use stop() to release hardware.

Example 5 — Track Count Before and After

Log getTracks().length after each removal.

JavaScript
navigator.mediaDevices
  .getUserMedia({ audio: true, video: true })
  .then((stream) => {
    console.log("before:", stream.getTracks().length);

    stream.removeTrack(stream.getVideoTracks()[0]);
    console.log("after video:", stream.getTracks().length);

    stream.removeTrack(stream.getAudioTracks()[0]);
    console.log("after audio:", stream.getTracks().length);
  });
Try It Yourself

How It Works

Each removeTrack() shrinks the stream’s track set by one.

🚀 Common Use Cases

  • Detach a screen-share track when the user stops sharing (MDN-style composition).
  • Remove video from a stream while keeping audio for a voice-only mode.
  • Rebuild streams dynamically with addTrack/removeTrack pairs.
  • Clear all tracks from a composed stream before adding new ones.
  • WebRTC: remove a remote track reference when a peer disconnects.

🔧 How It Works

1

Have a MediaStream

With at least one track in its track set.

Context
2

Pass track reference

Call removeTrack(track) with a track currently in the stream.

Parameter
3

Track detached

Track no longer appears in getTracks() for this stream.

Effect
4

Optional stop()

Call track.stop() if you also need to release camera or microphone hardware.

📝 Notes

  • MDN: Baseline Widely available (since September 2017) — no Deprecated / Experimental / Non-standard banner.
  • Does not stop hardwareremoveTrack() only updates the stream; call track.stop() to end the track.
  • Track must be in stream — pass a track reference that belongs to this stream.
  • Empty stream — MDN: removing all tracks leaves getTracks() as [].
  • active property — stream may become inactive when no live tracks remain (see active).
  • Related learning: addTrack(), getTracks(), clone(), getUserMedia().

Universal Browser Support

MediaStream.removeTrack() is marked Baseline Widely available on MDN (since September 2017). Logos use the shared browser-image-sprite.png sprite from this project.

Baseline · Widely available

MediaStream.removeTrack()

Removes a MediaStreamTrack from the stream. Returns undefined.

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 No MediaStream.removeTrack
Not supported
MediaStream.removeTrack() Excellent

Bottom line: Call removeTrack(track) to detach a track from a MediaStream without necessarily stopping it.

Conclusion

MediaStream.removeTrack(track) detaches a MediaStreamTrack from a stream and returns undefined. MDN’s example removes audio and video until getTracks() is empty. Pair with addTrack() for manual composition, and call track.stop() when you need to release hardware.

Continue with addTrack(), getTracks(), getVideoTracks(), or the JavaScript hub.

💡 Best Practices

✅ Do

  • Call track.stop() when releasing camera/mic hardware
  • Use getTracks() after removal to verify the stream state
  • Pair removeTrack() with addTrack() for dynamic streams
  • Remove tracks from the correct stream object
  • Check active after removing the last live track

❌ Don’t

  • Assume removeTrack() turns off the camera or mic
  • Pass a track that is not in this stream
  • Confuse detach (removeTrack) with end (stop)
  • Forget to clean up tracks when leaving a page
  • Expect a return value from removeTrack()

Key Takeaways

Knowledge Unlocked

Five things to remember about removeTrack()

Detach tracks from a MediaStream.

5
Core concepts
🔗02

Returns

undefined

Result
📦03

Effect

detach

Action
📄04

MDN

empty []

Pattern
🎯05

Baseline

since Sep 2017

Status

❓ Frequently Asked Questions

removeTrack(track) removes a MediaStreamTrack from the stream's track set. The track parameter must be a MediaStreamTrack object that is currently in the stream.
No. MDN marks MediaStream.removeTrack() as Baseline Widely available (since September 2017). It is not Deprecated, Experimental, or Non-standard.
undefined. MDN documents no return value. Use getTracks() afterward to see what remains in the stream.
No. removeTrack() only detaches the track from this stream. The track object may still be live. Call track.stop() separately if you want to end the camera or microphone hardware.
addTrack(track) adds a track to the stream. removeTrack(track) removes it. They are inverse operations for composing streams manually.
After getUserMedia, copy tracks into a new MediaStream, then remove the video and audio tracks—getTracks() returns an empty array.
Did you know?

MDN’s removeTrack() example leaves newStream.getTracks() as an empty array—but the original MediaStreamTrack objects may still be live until you call stop() on them.

Track added events

Learn addtrack—react when a MediaStreamTrack joins a stream.

addtrack 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