Lodash _.unset() Method

Beginner
⏱️ 8 min read
📚 Updated: Jul 2026
🎯 6 Examples + 3 Try It
Object utilities

What You’ll Learn

By the end of this tutorial, you’ll use _.unset() to remove nested properties safely with dot paths and key arrays.

01

Core syntax

_.unset(object, path) deletes at any depth.

02

Mutates object

Modifies the target in place; returns boolean.

03

Path formats

Dot strings, key arrays, and users[0] notation.

04

Safe removal

No throw when the path is missing—returns false.

05

unset vs set

Pair deletes with _.set() writes.

06

Data cleaning

Strip passwords, temp fields, and nested secrets.

What Is _.unset()?

_.unset() is Lodash’s nested delete helper. You pass an object and a path; Lodash walks to that location and removes the property if it exists. It is the removal counterpart to _.set() and uses the same path syntax as _.get().

💡
Unlike _.omit()

_.omit() drops top-level keys and returns a new object. _.unset() targets nested paths and mutates the object you pass in.

Use it to strip sensitive fields before API responses, remove obsolete config keys, clean dynamic object trees, and delete nested values without fragile delete obj.a.b.c chains that throw when intermediates are missing.

📝 Syntax

The signature is two arguments—object and path:

javascript
_.unset(object, path)

Syntax Rules

  • object — the object to modify (mutated in place).
  • path — dot string ("address.zip"), array (["profile", "temp"]), or bracket form ("users[0].email").
  • Return valuetrue if the property existed and was removed; false otherwise.
  • Missing paths — no error thrown; the object stays unchanged and you get false.
  • Parent objects — remain after deletion; empty {} parents are not auto-removed.
javascript
import unset from "lodash/unset";

const user = {
  id: 1,
  name: "John",
  address: { city: "New York", zip: "10001" }
};

unset(user, "address.zip");

// user -> { id: 1, name: "John", address: { city: "New York" } }

⚡ Quick Reference

TaskCode patternResult
Nested dot path_.unset(obj, "address.zip")Removes nested key
Array of keys_.unset(obj, ["profile", "temp"])Same as dot path
Array index_.unset(obj, "users[0].email")Removes nested array field
Check before deleteif (_.has(obj, path)) _.unset(obj, path)Optional guard
Return valueconst ok = _.unset(obj, path)true / false
Top-level keys_.omit(obj, ["email"])See _.omit()
Mutates?
Yes

In-place delete

Missing path
false

No throw

Returns
boolean

Success flag

Pair with
_.set()

Write at path

🧰 Parameters

Arguments to _.unset() and how Lodash applies them:

object Required

The target object. Lodash mutates this object directly when the path resolves.

const user = { address: { zip: "10001" } };
_.unset(user, "address.zip")
path Required

Location to delete: string path, bracket notation, or array of keys. Same formats as _.set and _.get.

_.unset(o, "profile.temp")
_.unset(o, ["profile", "temp"])
return value Output

true if the property existed and was removed; false if the path could not be resolved.

const removed = _.unset(user, "address.zip")
// true
parent objects Behavior

After deleting a leaf, parent containers remain—even empty {} objects are not auto-pruned.

_.unset({ a: { b: 1 } }, "a.b")
// { a: {} }

Use _.has() when you need to confirm a path exists before removing it. For immutable workflows, clone first with _.cloneDeep().

Examples Gallery

Practical _.unset() patterns with sample output and interactive Try It Yourself labs.

📚 Getting Started

Remove nested properties with dot paths and key arrays.

Example 1 — Remove a nested address field

Delete address.zip from a user object while keeping other nested keys.

javascript
const user = {
  id: 1,
  name: "John",
  address: { city: "New York", zip: "10001" }
};

_.unset(user, "address.zip");

console.log(user);
// -> { id: 1, name: "John", address: { city: "New York" } }
Try It Yourself

How It Works

Lodash walks to address, deletes zip, and returns true. The parent address object remains.

Example 2 — Strip sensitive data before sending

Remove a top-level password field from an API payload.

javascript
const data = {
  id: 1,
  name: "Alice",
  email: "alice@example.com",
  password: "hashedpassword123"
};

_.unset(data, "password");

console.log(data);
// -> { id: 1, name: "Alice", email: "alice@example.com" }
Try It Yourself

📈 Practical Patterns

Deep paths, array indices, dynamic paths, and comparisons.

Example 3 — Deep nested removal

Delete a leaf value several levels deep with a dot path.

javascript
const nested = {
  a: { b: { c: "Nested Value" } }
};

_.unset(nested, "a.b.c");

console.log(nested);
// -> { a: { b: {} } }

Parent objects stay behind—Lodash does not prune empty {} containers automatically.

Example 4 — Remove from array elements

Use bracket notation to delete a nested field on the first user in an array.

javascript
const data = {
  users: [
    { name: "Alice", email: "alice@example.com" },
    { name: "Bob", email: "bob@example.com" }
  ]
};

_.unset(data, "users[0].email");

console.log(data.users[0]);
// -> { name: "Alice" }
Try It Yourself

Example 5 — Dynamic path variable

Build the path at runtime and remove a settings key safely.

javascript
const settings = {
  theme: "dark",
  showNotifications: true,
  enableAnalytics: false
};

const pathToRemove = "showNotifications";

if (!settings.showNotifications) {
  _.unset(settings, pathToRemove);
}

console.log(settings);
// -> { theme: "dark", enableAnalytics: false }

🚀 Beyond the Basics

When unset fits better than omit or manual delete.

Example 6 — _.unset() vs _.omit()

_.omit() returns a new object with top-level keys removed; _.unset() mutates at any path.

javascript
const user = {
  name: "John",
  profile: { temp: "draft", bio: "Dev" }
};

const viaOmit = _.omit(user, ["name"]);
_.unset(user, "profile.temp");

console.log(viaOmit);
// -> { profile: { temp: "draft", bio: "Dev" } }

console.log(user);
// -> { name: "John", profile: { bio: "Dev" } }

🧠 How _.unset() Works

1

Parse path

Lodash normalizes the path string or key array into segments, same as _.set.

Input
2

Walk to parent

Each segment is followed until the parent of the target key is reached.

Traverse
3

Delete property

If the final key exists, it is removed with delete and the method returns true.

Remove
=

Boolean result

true if removed; false if the path could not be resolved.

📝 Notes

  • _.unset() mutates the target object—clone first when immutability matters.
  • Returns true or false; it does not return the object (unlike _.set()).
  • Missing paths are safe—no exception is thrown when intermediates or the final key are absent.
  • Empty parent objects remain after deleting a leaf; prune them separately if needed.
  • Use the same path formats as _.set() and _.get().
  • For top-level key removal without mutation, prefer _.omit().

Conclusion

_.unset() is the safe way to delete nested properties without manual delete obj.a.b chains that break when intermediates are missing. Pair it with _.set() for write/delete symmetry and check return values when you need to know whether a key actually existed.

Clone before unsetting when immutability matters. Next in the series: _.update() for applying a function at a nested path.

💡 Best Practices

✅ Do

  • Use _.has() when you need to confirm a path before deleting
  • Match path style with _.set() and _.get() across your codebase
  • Clone before _.unset() when immutability is required
  • Check the boolean return value when removal success matters
  • Use _.omit() for shallow, non-mutating key removal

❌ Don’t

  • Assume _.unset() returns a new object like _.omit()
  • Expect empty parent objects to be auto-removed after deletion
  • Confuse _.unset() with immutable delete patterns
  • Unset paths on shared references without considering side effects
  • Rely on unset to remove multiple keys—loop or use _.omit() instead

Key Takeaways

Knowledge Unlocked

Five things to remember about _.unset()

Use these when deleting nested properties safely.

5
Core concepts
⚠️ 02

Mutates

In-place removal.

Important
03

Boolean

true / false.

Return
🔀 04

vs set

Delete / write pair.

Related
🛡️ 05

Safe path

No throw.

Tip

❓ Frequently Asked Questions

_.unset() removes the property at a nested path on an object. It mutates the object in place and returns true if the property existed and was deleted, or false if the path could not be resolved.
Yes. _.unset() always modifies the object you pass in, just like _.set(). To avoid mutation, clone first: _.unset(_.cloneDeep(obj), path) or work on a copy of your data.
The same formats as _.set() and _.get(): dot-path strings like address.zip, bracket notation like users[0].email, or key arrays like ["profile", "temp"].
Lodash returns false and leaves the object unchanged. Unlike manual delete chains, _.unset() does not throw when intermediate or final segments are missing.
_.omit() removes top-level keys by name and returns a new object. _.unset() deletes at any nested path and mutates the target object in place.
No. After removing a leaf property, parent objects remain—even if they become empty {}. Clean up empty containers separately if your app requires it.
Did you know?

Some older tutorials claim _.unset() is non-mutating and returns a new object—that is incorrect. Like _.set(), it modifies the object you pass in. The return value is a boolean success flag, not a copy of the data.

Practice _.unset() in the Live Editor

Remove nested fields, strip sensitive data, and test missing paths safely.

Open Try It editor →

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.

6 people found this page helpful