DeleteCases[expr,pattern]
removes all elements of expr that match pattern.
DeleteCases[expr,pattern,levelspec]
removes all parts of expr on levels specified by levelspec that match pattern.
DeleteCases[expr,pattern,levelspec,n]
removes the first n parts of expr that match pattern.
DeleteCases[pattern]
represents an operator form of DeleteCases that can be applied to an expression.
DeleteCases
DeleteCases[expr,pattern]
removes all elements of expr that match pattern.
DeleteCases[expr,pattern,levelspec]
removes all parts of expr on levels specified by levelspec that match pattern.
DeleteCases[expr,pattern,levelspec,n]
removes the first n parts of expr that match pattern.
DeleteCases[pattern]
represents an operator form of DeleteCases that can be applied to an expression.
Details and Options
- When used on an Association, DeleteCases deletes elements according to their values.
- DeleteCases[pattern][expr] is equivalent to DeleteCases[expr,pattern].
- With the option Heads->True, you can delete heads with DeleteCases. Deleting the head of a particular element in an expression is equivalent to applying FlattenAt to the expression at that point.
- DeleteCases uses standard level specifications:
-
n levels 1 through n Infinity levels 1 through Infinity {n} level n only {n1,n2} levels n1 through n2 - The default value for levelspec in DeleteCases is {1}.
- A positive level n consists of all parts of expr specified by n indices.
- A negative level -n consists of all parts of expr with depth n.
- Level -1 consists of numbers, symbols, and other objects that do not have subparts.
- Level 0 corresponds to the whole expression.
- DeleteCases traverses the parts of expr in a depth-first order, with leaves visited before roots.
Examples
open all close allBasic Examples (5)
Delete cases that match integers:
DeleteCases[{1, 1, x, 2, 3, y, 9, y}, _Integer]DeleteCases[_Integer][{1, 1, x, 2, 3, y, 9, y}]Delete elements from Association:
DeleteCases[<|"a" -> 1, "b" -> 2, c -> "3", d -> 4|>, _Integer]DeleteCases can access parts of the elements of an Association:
DeleteCases[<|"a" -> 1, "b" -> 2, c -> {"3", 4}, d -> 4|>, _Integer, {2}]Deleting elements from a nested Association:
DeleteCases[<|"a" -> 1, "b" -> <|"3" -> 3, "4" -> "4", "5" -> <|f -> 1|>|>, "c" -> 4|>, _Integer, {2, 3}]Scope (2)
Delete elements matching an Association from a list:
DeleteCases[{<|a -> 1|>, <|b -> 2|>}, <|a -> _|>]Delete elements from a list matching a condition:
DeleteCases[{<|a -> 1|>, <|b -> 2|>}, <|_ -> a_ /; EvenQ[a]|>]DeleteCases[{<|a -> 1|>, <|b -> 2|>}, <|a -> _ ? OddQ|>]Options (1)
Applications (1)
Build a function that works like Complement, but preserving order and multiplicity:
UnorderedComplement[list1_List, list2_List] := DeleteCases[list1, Alternatives@@list2]UnorderedComplement[{b, e, d, a, b, c, d}, {b, c}]Complement[{b, e, d, a, b, c, d}, {b, c}]Possible Issues (1)
Deleting the head effectively flattens the expression:
DeleteCases[{1, f[2, 3], 4}, f, {2}, Heads -> True]Deleting the head in Association removes the entry:
DeleteCases[<|"a" -> 1, "b" -> f["2", 3], "c" -> 4|>, f, {2}, Heads -> True]Tech Notes
Related Links
History
Introduced in 1991 (2.0) | Updated in 2014 (10.0)
Text
Wolfram Research (1991), DeleteCases, Wolfram Language function, https://reference.wolfram.com/language/ref/DeleteCases.html (updated 2014).
CMS
Wolfram Language. 1991. "DeleteCases." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2014. https://reference.wolfram.com/language/ref/DeleteCases.html.
APA
Wolfram Language. (1991). DeleteCases. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/DeleteCases.html
BibTeX
@misc{reference.wolfram_2026_deletecases, author="Wolfram Research", title="{DeleteCases}", year="2014", howpublished="\url{https://reference.wolfram.com/language/ref/DeleteCases.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_deletecases, organization={Wolfram Research}, title={DeleteCases}, year={2014}, url={https://reference.wolfram.com/language/ref/DeleteCases.html}, note=[Accessed: 13-June-2026]}