JavaScript Remove Key Value from Object: Delete Key from Object in JavaScript
What are JavaScript objects? Objects are key—value maps. Removing a property deletes both the key and its value from that map.
Problem
- You need to remove a property (key) from a JavaScript object.
- You may want to mutate the original object or return a new object without the key.
Solutions
- Use
delete
to mutate the object in place.
const user = { id: 1, name: 'Ada', admin: true };
// by identifier
delete user.admin;
// by string key
delete user['admin'];
// by dynamic key
const k = 'name';
delete user[k]; // user is now { id: 1 }
- Create a new object without a key (immutable, keeps originals intact).
const user = { id: 1, name: 'Ada', admin: true };
// known key
const { admin, ...rest } = user; // rest -> { id: 1, name: 'Ada' }
// dynamic key
const keyToRemove = 'name';
const { [keyToRemove]: _, ...withoutName } = user; // { id: 1, admin: true }
- Use
Reflect.deleteProperty(obj, key)
when you want an explicit boolean result.
const user = { id: 1, role: 'editor' };
if (!Reflect.deleteProperty(user, 'role')) {
// property was non-configurable or not deletable
}
delete
isn’t the only way to remove a key from a plain JS object. It’s the most direct, but there are several alternative approaches depending on whether you want to mutate the object or return a new one. Here are the main categories you could add to your post:
1. Immutable Removal (return a new object)
This avoids mutating the original object, which is common in functional programming or state management (React/Redux, etc.).
Using object rest destructuring:
const obj = { a: 1, b: 2, c: 3 };
const { b, ...withoutB } = obj;
console.log(withoutB); // { a: 1, c: 3 }
With a dynamic key:
const keyToRemove = 'b';
const { [keyToRemove]: _, ...rest } = obj;
console.log(rest); // { a: 1, c: 3 }
2. Utility Functions (cloning + omit)
Manual omit with Object.fromEntries
:
const obj = { a: 1, b: 2, c: 3 };
const key = 'b';
const withoutB = Object.fromEntries(
Object.entries(obj).filter(([k]) => k !== key)
);
console.log(withoutB); // { a: 1, c: 3 }
Libraries like Lodash:
_.omit(obj, 'b')
does the same thing, widely used in older codebases.
3. Reflect.deleteProperty
A standards-based version of delete
that returns a boolean:
const obj = { x: 42 };
const removed = Reflect.deleteProperty(obj, 'x');
console.log(removed); // true
console.log(obj); // {}
4. Overwriting with a New Object
Instead of deleting, rebuild without the unwanted property:
let obj = { a: 1, b: 2, c: 3 };
obj = { a: obj.a, c: obj.c }; // manually pick properties
In TypeScript or schemas, this can enforce explicit field selection.
Delete Key from Object: JavaScript Summary
delete
/Reflect.deleteProperty
→ direct mutation.- Destructuring with rest → immutable, clean, ES6+.
- Object.entries + filter + Object.fromEntries → dynamic removal, functional style.
- Libraries (
_.omit
) → for utility-heavy codebases.
Things to Consider
delete
mutates the object; destructuring with rest returns a new object.- Deleting array indices leaves holes; use
splice
instead. delete
only removes own properties; it does not affect properties on the prototype.- Non-configurable properties cannot be deleted; the operation returns
false
. - TypeScript: deleting a non-optional property may error under
strictNullChecks
; mark it optional or use an immutable pattern.
Gotchas
- Setting
obj.key = undefined
does not delete the key; it only changes the value. - Deleting variables or function names (not object properties) is disallowed in strict mode.
- Keys with characters like hyphens require bracket notation:
delete obj['foo-bar']
. - For
Map
, usemap.delete(key)
;delete
is for plain objects.
Sources
- MDN:
delete
operator . - MDN:
Reflect.deleteProperty
. - MDN: Destructuring (object rest) .
- StackOverflow: How do I remove a property from a JavaScript object?
Further Investigation
- Property attributes:
configurable
andwritable
viaObject.getOwnPropertyDescriptor
. - Immutable update patterns in Redux—style state management.
- Proxies and
deleteProperty
traps. - MDN: Proxy
deleteProperty
handler
TL;DR
- Mutate:
delete obj[key];
- Immutable:
const { [keyToRemove]: _omit, ...next } = obj;