Sass map.remove() Function

Beginner
⏱️ 11 min read
📚 Updated: Jul 2026
🎯 5 Examples
sass:map · Dart Sass

What You’ll Learn

map.remove() belongs to the built-in sass:map module. It returns a new map with one or more top-level keys deleted. This page covers multi-key removal, missing keys, immutability, vs map.deep-remove, the legacy map-remove name, and five compiled examples.

01

Concept

Delete top-level keys

02

Module

@use "sass:map"

03

Many keys

One call, several removals

04

Missing

Ignored safely

05

vs deep-remove

Top-level vs nested path

06

Practice

5 examples

What Is map.remove()?

Design token maps grow over time. Sometimes you need a copy without a legacy flag, a deprecated color, or a weight you no longer ship. map.remove() does exactly that for top-level keys:

  • Pass the map, then every key you want gone
  • Get a new map back—the original stays unchanged
  • Keys that were never there are simply ignored

It does not walk into nested maps. To delete fonts → Helvetica → weights → regular, use map.deep-remove().

💡
Beginner tip

Think of a flat folder of files. map.remove deletes whole files at the root. It cannot open a subfolder and delete one file inside—that is map.deep-remove.

📝 Syntax

Load the map module, then call the function:

styles.scss
@use "sass:map";

map.remove($map, $keys...)

Parameters

ParameterTypeRequiredDescription
$mapMapYesSource map. Never mutated—you always get a new map back.
$keys...Any (variadic)Yes*One or more top-level keys to drop. Missing keys are ignored.

*You normally pass at least one key. Passing none returns a copy of the map unchanged.

Return value

TypeSituationResult
MapKey(s) existNew map without those top-level entries
MapKey missingThat key is skipped; other removals still apply
MapAll keys missingCopy of $map (same keys and values)

📦 Loading sass:map

Modern Sass groups map helpers into a module. Load it with @use, then call map.remove. The older global name map-remove still works.

styles.scss
// Recommended — namespaced
@use "sass:map";
$clean: map.remove($tokens, "legacy");

// Optional — bring members into the current namespace
@use "sass:map" as *;
$clean: remove($tokens, "legacy");

// Optional — custom namespace
@use "sass:map" as m;
$clean: m.remove($tokens, "legacy");

// Legacy global
$clean: map-remove($tokens, "legacy");
⚠️
Put @use first

@use rules must appear before most other rules. Keep @use "sass:map"; near the top of the file.

🔍 How Removal Works

Official behavior is easy to remember:

  • Only top-level keys are candidates for deletion
  • You can list many keys in one call
  • Unknown keys do not cause an error
  • Remaining keys keep their relative order
styles.scss
@use "sass:map";

$font-weights: ("regular": 400, "medium": 500, "bold": 700);

map.remove($font-weights, "regular");
// ("medium": 500, "bold": 700)

map.remove($font-weights, "regular", "bold");
// ("medium": 500)

map.remove($font-weights, "bolder");
// ("regular": 400, "medium": 500, "bold": 700) — unchanged

📋 map.remove vs map.deep-remove

Both return a new map without certain keys. The difference is depth.

map.removemap.deep-remove
TargetTop-level keys onlyTop-level or nested leaf via path
Extra argumentsMore keys to delete at the rootPath keys; last key is removed
Best forFlat token mapsNested theme / config trees
Legacy globalmap-removeNone—module only
Since (deep path)Long-standingDart Sass 1.27+
💡
Quick pick

Flat map like ("primary": …, "muted": …)? Use map.remove. Nested path like ("colors": ("accent": …)) and you only want accent gone? Use map.deep-remove.

🛠 Compatibility

This is about Sass compilers, not browsers. Compiled CSS only contains the finished values you read from the cleaned map (for example with map.get).

Implementation@use "sass:map"Global map-remove
Dart Sass (1.23+)Yes — preferredYes
LibSassNo built-in modulesYes (legacy)
Ruby SassNo built-in modulesYes (legacy)

Prefer Dart Sass for new work. Use map.deep-remove on Dart Sass 1.27+ when you need nested key paths.

⚡ Quick Reference

GoalCode
Import map@use "sass:map";
Remove one keymap.remove($map, "legacy")
Remove several keysmap.remove($map, "a", "b")
Nested leaf keymap.deep-remove($map, "a", "b", "leaf")
Keep result$map: map.remove($map, "temp");
Legacy global callmap-remove($map, "legacy")

Examples Gallery

Each example uses @use "sass:map". Open View Compiled CSS to see what Dart Sass emits (maps inspected via meta.inspect where needed).

📚 Getting Started

Official docs samples: one key, many keys, missing keys.

Example 1 — Remove One Key (Official Docs)

Drop regular from a font-weight map.

styles.scss
@use "sass:map";
@use "sass:meta";

$font-weights: ("regular": 400, "medium": 500, "bold": 700);

@debug map.remove($font-weights, "regular");
// ("medium": 500, "bold": 700)

.weights {
  --kept: #{meta.inspect(map.remove($font-weights, "regular"))};
}

How It Works

Sass copies the map without the "regular" entry. $font-weights itself is unchanged unless you reassign.

Example 2 — Remove Several Keys at Once

Pass every key you want deleted in a single call.

styles.scss
@use "sass:map";
@use "sass:meta";

$font-weights: ("regular": 400, "medium": 500, "bold": 700);

@debug map.remove($font-weights, "regular", "bold");
// ("medium": 500)

.weights {
  --medium-only: #{meta.inspect(map.remove($font-weights, "regular", "bold"))};
}

How It Works

Both "regular" and "bold" are stripped. Only "medium" remains.

📈 Practical Patterns

Safe missing keys, theme cleanup, and when to use deep-remove.

Example 3 — Missing Keys Are Ignored

Asking to remove a key that does not exist leaves the map intact for that key.

styles.scss
@use "sass:map";
@use "sass:meta";

$font-weights: ("regular": 400, "medium": 500, "bold": 700);

@debug map.remove($font-weights, "bolder");
// ("regular": 400, "medium": 500, "bold": 700)

.safe {
  --all: #{meta.inspect(map.remove($font-weights, "bolder"))};
  --partial: #{meta.inspect(map.remove($font-weights, "regular", "bolder"))};
}

How It Works

"bolder" is skipped. When mixed with a real key like "regular", only the real key is removed.

Example 4 — Clean Theme Tokens Before Emit

Strip internal flags, then read the remaining public tokens into CSS variables.

styles.scss
@use "sass:map";

$theme: (
  "primary": #1d4ed8,
  "muted": #94a3b8,
  "_debug": true,
  "_draft": "v2"
);

$public: map.remove($theme, "_debug", "_draft");

.theme {
  --primary: #{map.get($public, "primary")};
  --muted: #{map.get($public, "muted")};
}

How It Works

Internal keys never reach CSS. $theme can still keep them for other compile steps if you do not reassign over it.

Example 5 — Top-Level Remove vs Nested Deep-Remove

map.remove would delete the whole "weights" branch. map.deep-remove deletes only one nested weight.

styles.scss
@use "sass:map";
@use "sass:meta";

$fonts: (
  "weights": (
    "regular": 400,
    "medium": 500,
    "bold": 700
  ),
  "family": "Helvetica"
);

.compare {
  --top-level: #{meta.inspect(map.remove($fonts, "weights"))};
  --nested-leaf: #{meta.inspect(map.deep-remove($fonts, "weights", "regular"))};
}

How It Works

Top-level remove drops the entire nested map. Deep-remove keeps "weights" and only deletes "regular" inside it.

🚀 Real-World Use Cases

  • Strip internal flags — remove _debug / draft keys before emitting CSS variables.
  • Legacy cleanup — drop deprecated tokens from a shared palette map.
  • Feature subsets — build a lighter token map for a component package.
  • Safe optional deletes — list keys that might not exist; missing ones are ignored.
  • Flat maps only — pair with map.deep-remove when nesting is involved.

🧠 How Compilation Works

1

Write SCSS

Call map.remove($map, $key, …) after @use "sass:map".

Source
2

Keys are filtered

Dart Sass copies the map and drops each listed top-level key that exists.

Compile
3

New map returned

Read remaining tokens with map.get or loop with map.keys.

Result
4

CSS sees values

Browsers never see map.remove—only the finished CSS you emit.

⚠️ Common Pitfalls

  • Expecting nested deletesmap.remove($map, "colors", "accent") treats both as top-level keys to delete, not a path. Use map.deep-remove for paths.
  • Forgetting to reassign — remove returns a copy; originals stay unless you assign the result.
  • Deleting a whole branch by mistake — removing a nested map’s parent key drops every child token.
  • Assuming missing keys error — they are ignored; double-check spellings if nothing changed.
  • Forgetting @usemap.remove needs sass:map (or use legacy map-remove).

💡 Best Practices

✅ Do

  • Use @use "sass:map" and map.remove() in new SCSS
  • Pass every top-level key you want gone in one call
  • Choose map.deep-remove for nested leaf keys
  • Reassign when updating a stored config variable
  • Prefer Dart Sass for the module API

❌ Don’t

  • Expect the browser to evaluate map.remove
  • Treat extra arguments as a nested path (that is deep-remove)
  • Assume originals mutate without reassignment
  • Remove a parent key when you only meant to drop one child
  • Rely on errors to catch typos—missing keys are silent

Key Takeaways

Knowledge Unlocked

Five things to remember about map.remove()

Top-level key deletion—returns a new map; missing keys ignored.

5
Core concepts
📦 02

Module

sass:map

@use
🗑 03

Scope

top-level only

Rule
04

Missing keys

ignored safely

Safe
05

Legacy

map-remove still works

Tooling

❓ Frequently Asked Questions

map.remove($map, $keys...) returns a new map without the listed top-level keys. Other keys stay in place. It does not dig into nested maps—use map.deep-remove for nested paths.
No. It returns a copy. Reassign if you need to keep the result: $tokens: map.remove($tokens, "legacy").
Missing keys are ignored. The result is still a valid copy of the map (unchanged for those keys). No error is thrown.
Yes. Pass every key after the map: map.remove($map, "a", "b", "c").
Yes. map-remove($map, $keys...) is the older global name. Prefer map.remove after @use "sass:map".
map.remove only deletes top-level keys. map.deep-remove can walk a nested path and delete a leaf key while keeping parent maps and siblings.
Did you know?

Official Sass docs show three map.remove calls on the same font-weight map: remove one key, remove two keys, and remove a missing key. That third case is intentional—it teaches that unknown keys are ignored instead of throwing.

Conclusion

map.remove() is the simple way to drop top-level keys from a Sass map: list the keys, get a new map back, and ignore any names that were never there. Use it for flat token cleanup; switch to map.deep-remove when you need a nested path.

Continue with map.set() or the Sass introduction.

Next: set map keys

Learn map.set() to add or update tokens in a map.

map.set() →

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.

5 people found this page helpful