JavaScript Delete: Remove Property from Object
Learn javascript delete operator, Reflect.deleteProperty, object rest/spread to remove properties. Examples for js delete object, js delete object property, js object delete key without mutating.
How do I remove a property from a JavaScript object?
Given an object:
let myObject = {
"ircEvent": "PRIVMSG",
"method": "newURI",
"regex": "^http://.*"
};
How do I remove the property regex to end up with the following myObject?
let myObject = {
"ircEvent": "PRIVMSG",
"method": "newURI"
};
What are the different methods to delete a property from a JavaScript object, and what are the differences between them?
javascript delete (js delete object): the simplest way is the delete operator — for your example run delete myObject.regex; (or delete myObject['regex']) to remove the key in place. If you don’t want to mutate the original object, use object rest/spread, Object.assign/Object.fromEntries, or create a filtered copy; Reflect.deleteProperty(obj, 'regex') is a boolean-returning alternative to delete.
Contents
- Quick example: remove a property from an object
- Use the delete operator (javascript delete)
- Use Reflect.deleteProperty (js delete object)
- Create a new object without the property (js delete object property)
- Setting to undefined vs deleting (js object delete key)
- Arrays, prototype and performance caveats
- Sources
- Conclusion
Quick example: remove a property from an object
Given your object:
let myObject = {
ircEvent: "PRIVMSG",
method: "newURI",
regex: "^http://.*"
};
Quickest, in-place removal (mutates the object):
delete myObject.regex; // or: delete myObject['regex'];
console.log(myObject);
// { ircEvent: "PRIVMSG", method: "newURI" }
That delete call removes the regex own property from myObject. If you need a non‑mutating result, see the rest/spread and copy examples below.
Use the delete operator (javascript delete)
What it does
delete obj.propremoves an own property from an object (both dot and bracket notation work).- For properties created by an object literal (like your
regex),deletewill normally succeed and returntrue.
Syntax examples:
delete myObject.regex;
delete myObject['regex'];
const key = 'regex';
delete myObject[key];
Return value and strict mode
- In non-strict code
deletereturnstrueif the property was deleted or if the property didn’t exist; it returnsfalseif the property exists but is non-configurable. - In strict mode, attempting to delete a non-configurable property will throw a
TypeError. See the MDN page on the delete operator for details.
Own vs prototype
deleteonly affects own properties. If a property comes from the prototype chain,delete obj.propwon’t remove it from the prototype (delete would have to be used on the prototype object itself).
Check existence:
- After deleting,
myObject.hasOwnProperty('regex')isfalse, and'regex' in myObjectisfalse(unless the prototype defines it).
Use Reflect.deleteProperty (js delete object)
Reflect provides a function form:
const ok = Reflect.deleteProperty(myObject, 'regex'); // true/false
Differences vs delete
Reflect.deletePropertyalways returns a boolean reflecting the success of the operation; it won’t throw just because code is running in strict mode. That can be handy when you need a predictable result value from a function call.- Semantically it follows the same internal [[Delete]] semantics (so non-configurable properties still block deletion).
See the MDN entry for Reflect.deleteProperty.
Create a new object without the property (js delete object property)
If you prefer not to mutate the original object (common in functional code, React/Redux, etc.), make a copy that omits the key.
Object rest (ES2018+):
const { regex, ...withoutRegex } = myObject;
console.log(withoutRegex);
// { ircEvent: "PRIVMSG", method: "newURI" }
One-liner (useful for mapping/filtering):
const newObj = Object.fromEntries(
Object.entries(myObject).filter(([k]) => k !== 'regex')
);
Or copy + delete:
const copy = Object.assign({}, myObject);
delete copy.regex;
Notes
- Rest/spread and
Object.fromEntriesproduce new objects; originalmyObjectis unchanged unless you reassign it (myObject = withoutRegexifmyObjectislet). - Object rest/spread details: see MDN on object rest in destructuring and Object.fromEntries.
Setting to undefined vs deleting (js object delete key)
Setting a property to undefined is not the same as deleting it:
myObject.regex = undefined;
console.log(myObject.hasOwnProperty('regex')); // true
console.log('regex' in myObject); // true
console.log(JSON.stringify(myObject)); // regex may be omitted in JSON output
Key differences
deleteremoves the key entirely;undefinedkeeps the key with the valueundefined.Object.keys()andfor...inwill still list a property set toundefined.JSON.stringifytypically omits object properties withundefinedvalues (so for serialization there can be similar effects, but the runtime object shape is different).
If you want the property gone, use delete or create a filtered copy.
Arrays, prototype and performance caveats
Arrays
delete arr[index]removes the element but leaves a hole (sparse array). Thelengthis unchanged. Preferarr.splice(index, 1)to remove an element and shift indexes.
Prototype properties
- If a property is inherited from the prototype, deleting it from an instance won’t remove the prototype property. To remove it you must delete it from the prototype object itself (rarely desired).
Non-configurable properties
- Properties with
configurable: false(created viaObject.defineProperty) can’t be deleted. Deletion will fail (returnfalse) or throw in strict mode.
Performance
- Repeatedly adding/removing properties can change an object’s internal “shape” and may deoptimize JIT optimizations in some JS engines. If you need to remove lots of keys frequently, consider creating new objects (copy-on-write) or designing data structures to avoid churn.
When to mutate vs copy?
- Mutating with
deleteis fine for one-off changes or small scripts. - For predictable state updates (UI state, Redux, pure functions) prefer non-mutating copies.
Sources
- Yandex Wordstat
- MDN — delete operator
- MDN — Reflect.deleteProperty
- MDN — Object destructuring (rest in object destructuring)
- MDN — Object.fromEntries
- MDN — Object.assign
Conclusion
For a quick in-place removal use delete myObject.regex (javascript delete); if you want a boolean-returning function use Reflect.deleteProperty(myObject, 'regex'). Prefer a non‑mutating copy (object rest/spread, Object.fromEntries, or Object.assign + delete) when you need immutable updates. Set a property to undefined only when you intend the key to remain present — otherwise actual deletion is the right choice.