JavaScript WeakSet delete() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
WeakSet

Definition

weakSet.delete(value) removes an object from a WeakSet. It returns true when the object was in the set and is now gone, and false when there was nothing to remove. It never throws, even for numbers or strings. The object itself is not touched: it simply stops being a member of the set.

Remember
const done = new WeakSet();
const task = { id: 1 };
done.add(task);

done.delete(task)   → true  (it was there, now removed)
done.delete(task)   → false (already gone)
done.delete(5)      → false (not an object, no error)

task is still a normal object afterwards.

Syntax

Call delete on a WeakSet and pass the object you want to remove.

JavaScript
const weakSet = new WeakSet();
weakSet.delete(value)
CallWhat it does
weakSet.delete(value)Remove the object and return true or false.
if (weakSet.delete(obj)) { ... }Run code only when the object really was in the set.

Part of ES2015. A WeakSet has no clear() method, so delete() works on one object at a time.

Library declarations
JavaScript
weakSet.delete(value)   // returns true or false

// weakSet.clear()   does not exist
// To start over, make a new WeakSet.

Parameters

ParameterDescription
valueThe object to remove. Any other value, such as a number, string, null, or undefined, simply returns false and changes nothing.

Return Value

Return ValueDescription
trueThe object was in the WeakSet and has been removed.
falseThe object was not in the set, or the value was not an object at all.

JavaScript Examples

Example 1 — Remove an Object and Read the Result

Add a session object, remove it, and try to remove it a second time.

JavaScript
const active = new WeakSet();
const session = { user: "Ada" };
active.add(session);

console.log(active.delete(session));  // true
console.log(active.delete(session));  // false
console.log(active.has(session));     // false
console.log(session.user);            // Ada
Try It Yourself

How It Works

  1. First delete wins. The session is in the set, so the first delete() removes it and answers true.
  2. Second delete finds nothing. The set no longer holds it, so the next call answers false. No error, no warning.
  3. The object lives on. has() now says false, yet session.user still prints "Ada". Deleting only changes the set, never the object.

Example 2 — Toggle Selected Cards with the Return Value

One function selects a card on the first click and unselects it on the next. The result of delete() tells it which to do.

JavaScript
const selected = new WeakSet();

function toggle(card) {
  if (selected.delete(card)) {
    return "unselected";
  }
  selected.add(card);
  return "selected";
}

const card = { title: "Intro" };
console.log(toggle(card));        // selected
console.log(toggle(card));        // unselected
console.log(toggle(card));        // selected
console.log(selected.has(card));  // true
Try It Yourself

How It Works

  1. Ask by deleting. The if calls delete(card) first. A true means the card was selected, and it is now removed.
  2. Otherwise add it. On the first click delete() returns false, so the function falls through to add(card) and reports "selected".
  3. Three clicks, one state. Selected, unselected, selected again: after an odd number of clicks has(card) is true.

Example 3 — Wrong Values Return False

Deleting a look-alike object, a number, a string, or null does not throw. It just returns false.

JavaScript
const set = new WeakSet();
const saved = { id: 7 };
set.add(saved);

console.log(set.delete({ id: 7 }));  // false
console.log(set.delete(7));          // false
console.log(set.delete("id"));       // false
console.log(set.delete(null));       // false
console.log(set.has(saved));         // true
console.log(set.delete(saved));      // true
Try It Yourself

How It Works

  1. Look-alikes do not match. { id: 7 } typed a second time is a brand-new object, so delete() cannot find it and returns false.
  2. Primitives are ignored. A number, a string, and null are not objects. Unlike add(), delete() does not throw for them and answers false.
  3. The real one is still there. After four failed deletes, has(saved) is true. Only passing the original variable saved removes it.

Browser & Runtime Support

WeakSet.prototype.delete() is part of ES2015 (ES6) and ships together with WeakSet.

✓ Baseline · ES2015

WeakSet.prototype.delete()

Supported in Chrome 36+, Firefox 34+, Safari 9+, Edge 12+, Node.js, and Deno. Internet Explorer 11 has WeakMap but not WeakSet.

ES2015 Supported for years
Google Chrome 36 and newer
Supported
Mozilla Firefox 34 and newer
Supported
Apple Safari 9 and newer
Supported
Microsoft Edge 12 and newer
Supported
Node.js All current releases
Supported
Deno All releases
Supported
Internet Explorer No WeakSet, not even IE 11
Not supported
weakSet.delete() Very wide

Bottom line: Safe in every modern browser and runtime. There is nothing newer to worry about: delete() behaves the same everywhere WeakSet exists.

Frequently Asked Questions

It returns true when the object was in the set and has been removed, and false when it was not there. The false result is not an error, only a report that nothing changed.
No. It only removes the object from the set. Your variable still points to the object, and its properties are untouched.
No. Values that are not objects just return false. Only add() throws a TypeError for non-objects, while delete() and has() stay quiet.
There is no clear() method. Replace the set with a new one, for example let seen = new WeakSet(); and later seen = new WeakSet(). Objects in the old set stay out of the new one.
Not for memory. A WeakSet does not keep an object alive, so the engine can clean up an object nobody else uses. Call delete() when the object should no longer count as a member, for example after a task is finished.

Next: has()

Check whether an object is in the WeakSet without changing anything.

has() tutorial →

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.

11 people found this page helpful