Set.prototype.delete()
set.delete(value) The delete() method removes a specific value from a Set. It returns true if the value existed and was removed, or false if the value was not found. The Set’s size decreases by one on a successful deletion.
Value comparison uses the SameValueZero algorithm — the same equality check Set uses when adding values. This means -0 and +0 are treated as equal, but NaN is considered equal to itself (unlike ===).
Syntax
set.delete(value)
Parameters
| Parameter | Type | Description |
|---|---|---|
| value | any | The value to remove from the Set |
Return value
true if the value was in the Set and has been removed. false if the value was not found.
Examples
Basic usage
const numbers = new Set([1, 2, 3, 4, 5]);
const deleted = numbers.delete(3);
console.log(deleted);
// true
console.log(numbers.has(3));
// false
console.log(numbers.size);
// 4
When the value is present, delete() removes it, shrinks the Set by one, and returns true. A has() check afterward confirms the value is gone. But when the value was never in the Set, delete() returns false and the Set remains unchanged — no error, no exception.
Deleting non-existent values
const numbers = new Set([1, 2, 3]);
// Deleting a non-existent value returns false
console.log(numbers.delete(99));
// false
// Set remains unchanged
console.log([...numbers]);
// [1, 2, 3]
Checking the return value tells you whether the item was actually present. This is useful when you want to distinguish “was in the set and removed” from “was not there to begin with.” You can wrap delete() in a helper that inspects the return value and acts on it.
Conditional removal
const cache = new Set(['user:1', 'user:2', 'user:3', 'session:abc']);
// Only delete if it exists
function invalidate(key) {
if (cache.has(key)) {
cache.delete(key);
return true;
}
return false;
}
console.log(invalidate('user:1'));
// true
console.log(invalidate('user:99'));
// false
Since delete() already returns a boolean indicating success, calling has() first is optional. You can use the return value of delete() directly in the same way. For batch removal, pass an array of values to forEach and call delete() on each one.
Cleaning up a Set
const items = new Set(['a', 'b', 'c', 'd', 'e']);
// Remove multiple values
['a', 'c', 'e'].forEach(v => items.delete(v));
console.log([...items]);
// ['b', 'd']
Batch deletion with forEach works well when you have a fixed list of values to remove. For dynamic filtering based on a condition, you need to iterate the Set itself. The next example shows how to delete while looping.
Using delete in loops
const data = new Set([1, 2, 3, 4, 5]);
// Remove even numbers
for (const num of data) {
if (num % 2 === 0) {
data.delete(num);
}
}
console.log([...data]);
// [1, 3, 5]
Deleting from a Set while iterating it with for...of is safe. The iterator skips values that have been deleted and visits values that were present when iteration started. You can combine this with any condition — the next example filters scores below a threshold.
Clearing based on condition
const scores = new Set([10, 25, 30, 50, 100]);
// Remove all scores below 30
for (const score of scores) {
if (score < 30) {
scores.delete(score);
}
}
console.log([...scores]);
// [30, 50, 100]
delete() vs clear()
delete() removes a single value and returns a boolean. clear() removes all values at once and always returns undefined. Use delete() when you know which specific value to remove; use clear() when you want to empty the entire Set.
const s = new Set([1, 2, 3]);
s.delete(2); // removes 2, returns true
console.log([...s]); // [1, 3]
s.clear(); // removes all
console.log(s.size); // 0
Using delete() as a one-shot flag
A common pattern is to use a Set as a collection of “pending” items and delete() as the “consume” operation. Because delete() returns false on an absent value, you can detect when an item has already been processed without a separate has() call.
const pending = new Set(['task-a', 'task-b', 'task-c']);
function process(task) {
if (!pending.delete(task)) {
console.log(`${task} was already processed or unknown`);
return;
}
console.log(`Processing ${task}`);
// ... do work ...
}
process('task-a'); // Processing task-a
process('task-a'); // task-a was already processed or unknown
The delete() call both checks for membership and removes the item in a single atomic step, avoiding a separate has() check.
delete() and Identity
delete() uses the same equality rules as add(), which means the exact value identity matters. That is why NaN can be deleted from a Set even though NaN !== NaN, and why objects must match by reference rather than by shape. If you keep that identity rule in mind, the method is predictable: delete the same value you added, and the Set will shrink by one if that value is present.