Home Sass Map map.remove() Sass map.remove() Definition
Definition and Usage map.remove($map, $keys...) returns a new map without the listed top-level keys. Missing keys are ignored. Load it with @use "sass:map" on Dart Sass 1.23+ . Legacy global name: map-remove.
Official samples. map.remove(("regular": 400, "medium": 500, "bold": 700), "regular") → ("medium": 500, "bold": 700). Multiple keys: map.remove($map, "regular", "bold") → ("medium": 500).
Immutable result. Original maps stay unchanged. Reassign when you want to keep the cleanup: $tokens: map.remove($tokens, "legacy");
Syntax
Syntax @use "sass:map";
map.remove($map, $keys...); //=> map
map.remove($font-weights, "regular");
// ("medium": 500, "bold": 700)
map.remove($font-weights, "regular", "bold");
// ("medium": 500)
map.remove($font-weights, "bolder");
// unchanged copy — missing key ignored
// Legacy global (prefer module form)
// map-remove($map, $keys...); Pattern: drop unused top-level tokens with map.remove → confirm with map.has-key or map.keys .
Code
SCSS Examples Examples target Dart Sass 1.23+ with @use "sass:map". Open View Compiled CSS for verified output (matches official Sass docs samples).
1. Basic 2. Multiple keys 3. Missing key 4. vs deep-remove 5. Reassign Example 1 — Remove One Key (Official Docs) Drop a single top-level entry from a weight map.
@use "sass:map";
@use "sass:meta";
$font-weights: ("regular": 400, "medium": 500, "bold": 700);
$trimmed: map.remove($font-weights, "regular");
.one {
--result: #{meta.inspect($trimmed)};
--has-regular: #{map.has-key($trimmed, "regular")};
--medium: #{map.get($trimmed, "medium")};
} .one {
--result: ("medium": 500, "bold": 700);
--has-regular: false;
--medium: 500;
} How It Works regular is removed from the copy.Other keys remain in order.Original map is unchanged.Matches the official Sass docs sample.Example 2 — Remove Several Keys (Official Docs) Pass every key you want dropped in one call.
@use "sass:map";
@use "sass:meta";
$font-weights: ("regular": 400, "medium": 500, "bold": 700);
.multi {
--result: #{meta.inspect(map.remove($font-weights, "regular", "bold"))};
} .multi {
--result: ("medium": 500);
} How It Works Two keys are removed together.Only medium remains.Variadic API avoids chained removes.Matches the official multi-key sample.Example 3 — Missing Key Is Ignored (Official Docs) Unknown keys do not throw.
@use "sass:map";
@use "sass:meta";
$font-weights: ("regular": 400, "medium": 500, "bold": 700);
.missing {
--result: #{meta.inspect(map.remove($font-weights, "bolder"))};
} .missing {
--result: ("regular": 400, "medium": 500, "bold": 700);
} How It Works bolder is not in the map.Result looks unchanged for that key.Still a new map copy (safe to reassign).Matches the official missing-key sample.Example 4 — remove vs deep-remove Top-level remove deletes a whole nested branch.
@use "sass:map";
@use "sass:meta";
$theme: (
colors: (primary: navy, accent: teal),
space: (sm: 8px, md: 16px),
radius: 4px
);
$top: map.remove($theme, colors);
$leaf: map.deep-remove($theme, colors, accent);
.compare {
--remove-has-colors: #{map.has-key($top, colors)};
--remove: #{meta.inspect($top)};
--deep-primary: #{map.get($leaf, colors, primary)};
--deep-has-accent: #{map.has-key($leaf, colors, accent)};
} .compare {
--remove-has-colors: false;
--remove: (space: (sm: 8px, md: 16px), radius: 4px);
--deep-primary: navy;
--deep-has-accent: false;
} How It Works map.remove(..., colors) deletes the entire colors map.deep-remove drops only accent.primary survives with deep-remove.Pick based on depth of the token you want gone.Example 5 — Reassign After Cleanup Strip optional top-level groups and keep the trimmed map.
@use "sass:map";
$theme: (
colors: (primary: navy, accent: teal),
space: (sm: 8px, md: 16px),
radius: 4px
);
$theme: map.remove($theme, radius, space);
.reassign {
--has-space: #{map.has-key($theme, space)};
--has-radius: #{map.has-key($theme, radius)};
--has-colors: #{map.has-key($theme, colors)};
--primary: #{map.get($theme, colors, primary)};
} .reassign {
--has-space: false;
--has-radius: false;
--has-colors: true;
--primary: navy;
} How It Works Two top-level keys are removed in one call.Reassignment stores the cleaned map.colors remains for later reads.Useful when a project opts out of whole token groups. Edge Cases & Pitfalls Most remove bugs are using it for nested leaves, or forgetting to reassign.
nested Cannot target nested leaves map.remove($theme, accent) does nothing useful if accent lives under colors. Use deep-remove.
branch Removing a nested map key map.remove($theme, colors) deletes the entire colors branch, not one color token.
assign Returns a copy Callers must reassign: $map: map.remove($map, $keys...);
legacy Prefer map.remove over map-remove Global map-remove still works but is deprecated toward Dart Sass 3.0. Use the module API.
Toolchain
Sass Support Prefer the module form with a current Dart Sass release.
API Status map.remove()Dart Sass 1.23+ via @use "sass:map" Global map-remove() Works; deprecated toward Sass 3.0 Return type New map (input unchanged) Missing keys Ignored
Compile with the Dart Sass CLI .
Remember
Key Takeaways Deletes top-level keys and returns a new map.
Variadic — remove several keys in one call.
Use deep-remove for nested leaf keys.
Prefer map.remove over deprecated global map-remove.
One line: when a top-level map key must go, call map.remove($map, $keys...).
Practice
Compile Notes Practical checklist:
1. @use "sass:map"
2. Use remove for top-level keys; deep-remove for nested leaves
3. Pass multiple keys in one call when needed
4. Reassign: $map: map.remove($map, ...)
5. Confirm with map.has-key / map.keys Frequently Asked Questions What does Sass map.remove() do? 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.
Does map.remove change the original map? No. It returns a copy. Reassign if you need to keep the result: $tokens: map.remove($tokens, "legacy").
What if a key is missing? Missing keys are ignored. The result is still a valid copy of the map (unchanged for those keys). No error is thrown.
Can I remove several keys in one call? Yes. Pass every key after the map: map.remove($map, "a", "b", "c").
Is map-remove still valid? Yes. map-remove($map, $keys...) is the older global name, but Dart Sass deprecates global built-ins toward Sass 3.0. Prefer map.remove after @use "sass:map".
How is map.remove different from map.deep-remove? 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.
Next: Sass map.set() Set a value at a key—or nested path—and get a new map back.
map.set() → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful
Helpful Share Copy link Suggestion