handler.deleteProperty(target, property) is a trap that runs when you delete a property of a Proxy with the delete operator, or with Reflect.deleteProperty(). Instead of removing the property straight away, JavaScript first runs your trap.
The trap returns true when the property is gone (or was never there) and false when it could not be deleted. Inside the trap you can protect properties, log what was removed, or clean up. To delete in the normal way, use Reflect.deleteProperty(target, property).
Remember
const handler = {
deleteProperty(target, property) { ... }
};
target → the original object
property → the name (a string or a symbol)
return true → the property is gone
return false → not deleted (strict mode: TypeError)
Syntax
Add a deleteProperty trap to the handler of a Proxy.
JavaScript
const user = new Proxy({ id: 1, name: "Ann" }, {
deleteProperty(target, key) {
if (key === "id") {
return false; // protect id
}
return Reflect.deleteProperty(target, key);
}
});
console.log(delete user.name); // true
console.log(delete user.id); // false
Call
What it does
delete proxy.a
The delete operator runs the trap.
delete proxy["a"]
The same, with square brackets.
Reflect.deleteProperty(proxy, "a")
The function-style version. It returns true or false.
proxy.a = undefined
Does not run this trap. It changes the value and uses the set trap.
// ECMAScript 2015 (ES6)
handler.deleteProperty(target, property)
// target : the original object
// property : a string or a symbol
// returns : true or false
Parameters
Parameter
Description
target
The original object that the Proxy wraps.
property
The name of the property that is being deleted: a string or a symbol.
Return Value
Return Value
Description
true
The property was deleted, or it did not exist.
false
The property could not be deleted. In normal code delete then gives false. In strict mode it throws a TypeError.
TypeError (thrown)
The trap broke a rule, for example by returning true for a non-configurable property of the target.
JavaScript Examples
Example 1 — Protect Properties from delete
Return false for names that must stay. Here, id and every name that starts with an underscore are protected.
Check the name. The trap returns false for id and for any string that starts with "_". typeof key === "string" keeps symbols from breaking startsWith().
Delete the rest. For other names, Reflect.deleteProperty() removes the property from the target and returns true.
The target shows it. After the three deletes, only id and _token remain in the original object.
Example 2 — Strict Mode and Missing Properties
A refused delete is quiet in normal code, but throws in strict mode. Deleting something that does not exist counts as success. The catch block prints the error type only.
Normal delete: false
Strict delete: TypeError
Reflect on id: false
Missing property: true
id is still 1
How It Works
Same refusal, different reactions. The trap returns false for id. A normal delete gives false. The same delete in a strict function throws a TypeError.
Reflect gives a boolean.Reflect.deleteProperty() never throws for a refusal. It returns false, so you can react in your own way.
Missing is fine. Deleting zzz, which does not exist, returns true, because afterwards the property is gone.
Example 3 — Keep a Trash List and Respect the Rules
Remember what was deleted, so it can be restored later. Then see that a trap cannot claim a frozen property was removed.
Look before you delete. The trap reads target[key] and saves it in trash, then deletes the property for real.
Both deleted. After two deletes, the trash has both values and the object has no keys left.
No lying. The property x is non-configurable, so it cannot be removed. A trap that returns true anyway makes JavaScript throw a TypeError.
Compatibility
Browser & Runtime Support
handler.deleteProperty() is part of ES2015 (ES6), together with the Proxy object and the Reflect object. Every current browser and Node.js has it.
✓ ES2015 · Widely supported
handler.deleteProperty()
Supported in Chrome 49+, Edge 12+, Firefox 49+, Safari 10+, Opera 36+ and Node.js 6+. Internet Explorer never got it.
ES2015Standard since
Google Chrome49+ · Desktop & Mobile
Full support
Microsoft Edge12+ · Chromium and legacy
Full support
Mozilla Firefox49+ · Desktop & Mobile
Full support
Apple Safari10+ · macOS & iOS
Full support
Opera36+ · Chromium based
Full support
Samsung InternetRecent versions · Android
Supported
Android WebView49+ · Chromium based
Supported
Node.js6+ · Server runtime
Supported
DenoSupported · JavaScript runtime
Supported
BunSupported · JavaScript runtime
Supported
Internet ExplorerNo native support
Not supported
handler.deleteProperty()Excellent
Bottom line: Safe to use in every current browser and in Node.js. A Proxy cannot be faked in old browsers, so do not use it where Internet Explorer must work.
Frequently Asked Questions
It is a method of the handler object that runs when a property of a Proxy is deleted with the delete operator or with Reflect.deleteProperty(). It receives the target and the property name, and returns true or false.
The property stays. In normal code the delete expression gives false. In strict mode it throws a TypeError. Reflect.deleteProperty() just returns false.
No. Setting a value uses the set trap (or defineProperty). Only the delete operator and Reflect.deleteProperty() run this trap.
true. Reflect.deleteProperty() returns true for a missing property, and that is the normal answer, because after the call the property is gone.
The target property is non-configurable, so it cannot be removed. JavaScript checks that the trap does not claim something that is not true. Return false for such a property, or use Reflect.deleteProperty() to get the right answer.