JavaScript WeakMap delete() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
WeakMap

Definition

weakMap.delete(key) removes the entry for key and returns true. If the key was not stored, it does nothing and returns false. It never throws: a string, a number, or null just gives false. You do not need delete() to free memory, because a WeakMap lets go of dead keys on its own. Use it when the key object is still alive but you want to forget the data attached to it.

Remember
weakMap.delete(key)  → true or false

key was stored      → entry removed, true
key not stored      → nothing happens, false
key is not object   → false (no error)
second delete       → false

no need for memory: dead keys are cleaned up for you

Syntax

Call delete on a WeakMap with the key you want to forget.

JavaScript
weakMap.delete(key)
CallWhat it does
weakMap.delete(key)Remove the entry for key. Gives true if one was removed.
const removed = weakMap.delete(key)Keep the result when you need to know whether something was actually there.

Built into JavaScript since ES2015. There is no clear() on a WeakMap: to drop everything, create a new WeakMap and stop using the old one.

Library declarations
JavaScript
weakMap.delete(key)   // returns a boolean

// key: any value; only objects (and local symbols) can ever be removed
// never throws

Parameters

ParameterDescription
keyRequired. The object (or local symbol) whose entry should be removed. Any other value, including primitives, gives false.

Return Value

Return ValueDescription
trueAn entry for the key existed and has been removed.
falseThere was no entry for the key, or the value can never be a key.

JavaScript Examples

Example 1 — Delete Returns true, Then false

Remove an entry, check that it is gone, and try to remove it a second time.

JavaScript
const wm = new WeakMap();
const key = { id: 1 };
wm.set(key, "data");

console.log(wm.delete(key));   // true
console.log(wm.has(key));      // false
console.log(wm.delete(key));   // false
Try It Yourself

How It Works

  1. The first delete finds the entry. wm.delete(key) removes the entry that set() added, and reports it with true.
  2. The entry is really gone. Right after the removal, has(key) answers false.
  3. The second delete has nothing to do. The same call a second time finds no entry, changes nothing, and returns false instead of an error.

Example 2 — Remove One Entry, Keep the Others

Start with a WeakMap that holds two entries. Delete one, then try a few keys that are not stored.

JavaScript
const a = {};
const b = {};
const wm = new WeakMap([[a, "A"], [b, "B"]]);

console.log(wm.delete(a));      // true
console.log(wm.get(b));         // B
console.log(wm.delete({}));     // false
console.log(wm.delete("a"));    // false
console.log(wm.delete(null));   // false
Try It Yourself

How It Works

  1. Start with two pairs. The constructor receives a list of [key, value] pairs, so a and b are both stored from the start.
  2. Only the chosen entry goes. delete(a) removes A, and get(b) still returns "B".
  3. Strangers return false. A new empty object, a string, and null are not keys in this WeakMap, so each delete() answers false without any error.

Example 3 — Forget an Object's Data on Purpose

A timer keeps its count in a WeakMap. Calling dispose() removes the count while the timer object is still around.

JavaScript
const state = new WeakMap();

class Timer {
  constructor() {
    state.set(this, { ticks: 0 });
  }
  tick() {
    const s = state.get(this);
    if (!s) {
      return "disposed";
    }
    s.ticks += 1;
    return "ticks " + s.ticks;
  }
  dispose() {
    return state.delete(this);
  }
}

const t = new Timer();
console.log(t.tick());      // ticks 1
console.log(t.tick());      // ticks 2
console.log(t.dispose());   // true
console.log(t.tick());      // disposed
console.log(t.dispose());   // false
Try It Yourself

How It Works

  1. State lives in the map. The constructor stores { ticks: 0 } with this as the key, and each tick() reads and updates it.
  2. dispose() forgets the state. state.delete(this) removes the entry and returns true, while the timer object itself is untouched.
  3. The object notices. After that, get(this) finds nothing, so tick() answers "disposed", and a second dispose() returns false.

Browser & Runtime Support

WeakMap.prototype.delete() is part of ES2015 (ES6), together with Map, Set, and WeakSet.

✓ Baseline · ES2015

WeakMap.prototype.delete()

Works in every modern browser, plus Node.js, Deno, and Bun. Internet Explorer 11 has a basic WeakMap with set(), get(), has(), and delete(), but not the constructor argument.

98% Modern browser support
Google Chrome 36+ · Desktop & Mobile
Full support
Mozilla Firefox 6+ · Desktop & Mobile
Full support
Apple Safari 8+ · macOS & iOS
Full support
Microsoft Edge 12+
Full support
Node.js 0.12+ · every current release
Supported
Deno All releases
Supported
Internet Explorer IE 11 · no constructor argument
Basic
WeakMap.prototype.delete() Excellent

Bottom line: Safe to use in any modern browser and runtime. delete is a reserved word, but it is fine as a method name, so wm.delete(key) works everywhere WeakMap does.

Frequently Asked Questions

It removes the entry for a key from the WeakMap. It returns true if an entry was removed and false if there was nothing to remove.
No. A WeakMap does not keep its keys alive. When nothing else uses a key, the key and its entry can be cleaned up automatically. Use delete() only when you want to forget the data while the key is still in use.
No. Plain values can never be keys, so delete() simply returns false.
You cannot: a WeakMap has no clear() method. Create a new WeakMap with new WeakMap() and use that instead. The old one is cleaned up once nothing uses it.
No. It only removes the WeakMap entry. The key object and any other references to it are untouched.

Next: get()

Read values back from the WeakMap and see what a missing key returns.

get() 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