JavaScript MediaStream removeTrack() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Method

Definition

MediaStream.removeTrack(track) removes a MediaStreamTrack from the stream’s track set. Per MDN, it returns undefined — it does not stop the track’s media source.

Pass the required arguments shown below. Check return values and browser support before you rely on the result.

Syntax

Call stream.removeTrack. Start with this line.

JavaScript
mediaStream.removeTrack(track)
CallWhat it does
mediaStream.removeTrack(track)Primary signature.
stream.removeTrack(...)Call with the arguments your case needs.

Built into the browser. Call removeTrack(...) on a MediaStream — no import required.

Library declarations
JavaScript
mediaStream.removeTrack(track)

Parameters

ParameterDescription
trackRequired. MediaStreamTrack to remove

Return Value

Return ValueDescription
*Always undefined

JavaScript Examples

Example 1 — Basic removeTrack()

Add a canvas track, then remove it so the stream is empty.

JavaScript
const stream = new MediaStream();
const track = document.createElement("canvas")
  .captureStream().getVideoTracks()[0];

stream.addTrack(track);
console.log(stream.getTracks().length); // 1

stream.removeTrack(track);
console.log(stream.getTracks().length); // 0
Try It Yourself

How It Works

  1. Start empty. new MediaStream() has no tracks.
  2. Add a canvas track. Length becomes 1.
  3. Call removeTrack. Pass the same track object.
  4. Stream is empty. getTracks().length is 0.

Example 2 — Remove Video, Keep Audio

Detach only the video track — audio stays in the stream.

JavaScript
const ctx = new AudioContext();
const audio = ctx.createMediaStreamDestination()
  .stream.getAudioTracks()[0];
const video = document.createElement("canvas")
  .captureStream().getVideoTracks()[0];

const stream = new MediaStream([audio, video]);
stream.removeTrack(video);

console.log(stream.getVideoTracks().length); // 0
console.log(stream.getAudioTracks().length); // 1

ctx.close();
Try It Yourself

How It Works

  1. Compose both kinds. One audio + one video track.
  2. Remove only video. Pass the video track object.
  3. Video count is 0. Gone from this stream.
  4. Audio remains. Length of audio tracks stays 1.

Example 3 — Return Value Is undefined

Do not chain on the result — check getTracks() instead.

JavaScript
const stream = document.createElement("canvas").captureStream();
const track = stream.getTracks()[0];
const result = stream.removeTrack(track);

console.log(result); // undefined
console.log(stream.getTracks().length); // 0
Try It Yourself

How It Works

  1. Capture a stream. One canvas video track.
  2. Remove it. Store the return value.
  3. Result is undefined. MDN: no return value.
  4. Verify with getTracks(). Length is now 0.

Example 4 — Remove Does Not Stop

After detach, the track can still be "live".

JavaScript
const stream = document.createElement("canvas").captureStream();
const track = stream.getTracks()[0];

stream.removeTrack(track);

console.log(stream.getTracks().length); // 0
console.log(track.readyState);          // "live"

track.stop();
console.log(track.readyState);          // "ended"
Try It Yourself

How It Works

  1. Remove from the stream. Track set is empty.
  2. Check readyState. Still "live" — source not ended.
  3. Call stop(). Ends the media source.
  4. Now "ended". Use both APIs when you need detach + cleanup.

Example 5 — Missing Track Is a No-Op

Removing a track that is not in the stream does nothing.

JavaScript
const stream = document.createElement("canvas").captureStream();
const other = document.createElement("canvas")
  .captureStream().getVideoTracks()[0];

stream.removeTrack(other);

console.log(stream.getTracks().length); // 1
Try It Yourself

How It Works

  1. Build stream A. Has its own canvas track.
  2. Create track B. From a different capture — not in A.
  3. Call removeTrack(B). B was never in A.
  4. Length stays 1. Quiet no-op, no throw.

Browser Support

MediaStream.removeTrack() is Baseline Widely available (MDN: since September 2017). It is part of the Media Capture and Streams specification. It is a browser Web API — not available in Node.js, Bun, or Deno.

✓ Baseline · Widely available

MediaStream.removeTrack()

Supported across modern Chrome, Firefox, Safari, and Edge for removing tracks from a stream.

98% Browser support
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Android WebView Supported · Modern WebView
Full support
removeTrack() Excellent

Bottom line: Browser only — not a Node.js API. Safe for production. Use it to detach tracks; pair with track.stop() when you also need to end the source.

Frequently Asked Questions

removeTrack(track) removes a MediaStreamTrack from the stream’s track set. Pass the track 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.
No. It only detaches the track from this stream. 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.
The call is a no-op — the track set is unchanged and it does not throw.

Next: removetrack event

Listen when a track is removed from a MediaStream.

removetrack →

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.

10 people found this page helpful