C# List RemoveAll() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
List

Definition

In C#, List.RemoveAll(match) deletes every item that passes a test. The test is a predicate: a small function that takes one item and answers true or false. You usually write it as a lambda, such as x => x < 10, which reads "x goes to: x is less than 10".

Items for which the test says true are removed, and the rest keep their order. The method returns an int, the number of items it removed. It is the safe, short way to write a backwards removal loop with RemoveAt().

This is an instance member of List<T> (System.Collections.Generic). Call it on a list variable, such as numbers.RemoveAll(...). A List<T> is a class, so two variables can share one list. It keeps items in the order you put them, allows duplicates, and grows when needed. An array, by comparison, has a fixed length.

Syntax

Pass a lambda that returns true for items you want to delete.

C#
List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6 };
int removed = numbers.RemoveAll(n => n % 2 == 0);   // removed is 3
// numbers is now 1, 3, 5
CallWhat it does
numbers.RemoveAll(n => n < 10)Deletes every number below 10. Returns how many were deleted.
names.RemoveAll(string.IsNullOrEmpty)Pass a method name instead of a lambda. Deletes all empty or null strings.
numbers.RemoveAll(n => false)Matches nothing, so nothing is removed and the result is 0.

RemoveAll is O(n): it moves the kept items once, which is faster than calling Remove in a loop. A null predicate throws ArgumentNullException. Do not change the list from inside the predicate. The predicate should only look at the item and return a bool.

Library declarations
C#
public int RemoveAll(Predicate<T> match);

Parameters

ParameterDescription
matchRequired. A Predicate<T>: a lambda or method that takes an item and returns true for items to remove. It must not be null.

Return Value

Return ValueDescription
intThe number of items removed. It is 0 when no item matched, and then the list is unchanged.

C# Examples

Example 1 — Remove All Even Numbers

Use a lambda to delete every even number and print how many were removed.

C#
using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6 };

        int removed = numbers.RemoveAll(n => n % 2 == 0);

        Console.WriteLine(removed);
        Console.WriteLine(string.Join(", ", numbers));
        Console.WriteLine(numbers.Count);
    }
}

How It Works

  1. Read the lambda. n => n % 2 == 0 is true when n divides by 2 with no remainder, which is the definition of an even number.
  2. Test every item. RemoveAll runs the lambda on 1 through 6. It says true for 2, 4, and 6.
  3. Return value. Three items passed, so the method returns 3. You can ignore the number when you do not need it.
  4. Order of the rest. The survivors 1, 3, 5 keep their original order, and Count is now 3.

Example 2 — Remove Empty and Blank Strings

Delete every entry that is empty or only spaces by passing a ready-made test method.

C#
using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        List<string> words = new List<string> { "red", "", "green", "  ", "blue" };

        int removed = words.RemoveAll(string.IsNullOrWhiteSpace);

        Console.WriteLine(removed);
        Console.WriteLine(string.Join("|", words));
    }
}

How It Works

  1. Method as a test. string.IsNullOrWhiteSpace already takes a string and returns a bool, so it fits the predicate shape. No lambda is needed.
  2. Two blanks. The empty string and the string with two spaces both pass the test, so two items are removed.
  3. Words stay. "red", "green", and "blue" fail the test and stay in their original order.
  4. Visible check. Joining with | makes it easy to see that no blank entry is left between the words.

Example 3 — Remove Using a Limit Variable

A lambda can read variables from the code around it. Use one to choose the cut-off, and check the zero case.

C#
using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        List<int> prices = new List<int> { 20, 80, 45, 120, 60 };
        int limit = 50;

        int removed = prices.RemoveAll(p => p < limit);
        Console.WriteLine(removed + " removed: " + string.Join(", ", prices));

        int again = prices.RemoveAll(p => p < limit);
        Console.WriteLine(again + " removed: " + string.Join(", ", prices));
    }
}

How It Works

  1. Lambda sees limit. p => p < limit uses the outside variable limit. Change it once and the rule changes everywhere it is used.
  2. Two cheap items. 20 and 45 are below 50, so they are removed. 80, 120, 60 stay in order.
  3. Second call. Every remaining price is 50 or more, so the test never says true.
  4. Zero is normal. The method returns 0 and leaves the list as it was. No exception is thrown when nothing matches.

Frequently Asked Questions

It removes every item for which your predicate returns true, and it returns the number of items it removed.
A predicate is a small function that takes one item and returns true or false. In C# it is usually written as a lambda, such as x => x > 10.
It returns 0 and the list stays the same. No exception is thrown.
Usually yes. It is shorter, avoids index mistakes, and moves the kept items only once, so it is faster for big lists.
No. Arrays have a fixed length. Use a List<T>, or filter the array with LINQ Where and create a new array.

Next: RemoveAt()

Remove the item at a given index and move later items one place to the left.

RemoveAt() tutorial →

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.

12 people found this page helpful