jstedfast/MailKit · error · ArgumentException

Cannot search for an empty set of unique identifiers.

Error message

Cannot search for an empty set of unique identifiers.

What it means

MailKit's UidSearchQuery(IList<UniqueId>) constructor rejects an empty list because an IMAP UID SEARCH with zero UIDs is meaningless — the server would return nothing or error. The constructor validates uids != null and uids.Count > 0 before building the query. Passing an empty collection is treated as a programming mistake rather than a no-op search.

Solutions

  1. Check the collection count before constructing: skip the search (or use a query that matches everything/nothing intentionally) when uids.Count == 0.
  2. Ensure the upstream code that populates the UID list is correct and only invoke UidSearchQuery when at least one UID exists.
  3. If an empty result is legitimate, guard with an early return so the search (and its EmptyFolder/expunge logic) is never issued.

Example fix

// before
var query = searchAndDelete ? MailKit.Search.SearchQuery.Uids(uids) : null; // throws when uids is empty

// after
var query = uids.Count > 0 ? MailKit.Search.SearchQuery.Uids(uids) : null;
Defensive patterns

Strategy: validation

Validate before calling

if (uids != null && uids.Count > 0)
    folder.Search(SearchQuery.Uids(uids));

Type guard

bool HasUids(IList<UniqueId> uids) => uids != null && uids.Count > 0;

Try / catch

try {
    folder.Search(SearchQuery.Uids(uids));
} catch (ArgumentException ex) when (ex.ParamName == "uids") {
    // empty UID set: skip search or fall back to alternate query
}

Prevention

When it happens

Trigger: Calling new UidSearchQuery(new List<UniqueId>()) or new UidSearchQuery(new UniqueIdSet()) — i.e., constructing a UID search query from a collection that has zero elements, typically a result of an earlier fetch/search that returned no messages.

Common situations: Building searches dynamically from a list of message UIDs gathered earlier (e.g., a previous Search or Fetch), where the earlier step matched nothing; filtering UIDs through a predicate that removed all entries; deserializing persisted UID lists that are empty.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


AI-assisted analysis of jstedfast/MailKit@9d3859a785 (2026-09-15). Data as JSON: /api/errors/d71d8843597ea9ff. Report an issue: GitHub.

Appendix: source

Thrown at MailKit/Search/UidSearchQuery.cs:59

		/// Initializes a new instance of the <see cref="T:MailKit.Search.UidSearchQuery"/> class.
		/// </summary>
		/// <remarks>
		/// Creates a new unique identifier-based search query.
		/// </remarks>
		/// <param name="uids">The unique identifiers to match against.</param>
		/// <exception cref="System.ArgumentNullException">
		/// <paramref name="uids"/> is <see langword="null" />.
		/// </exception>
		/// <exception cref="System.ArgumentException">
		/// <paramref name="uids"/> is empty.
		/// </exception>
		public UidSearchQuery (IList<UniqueId> uids) : base (SearchTerm.Uid)
		{
			if (uids == null)
				throw new ArgumentNullException (nameof (uids));

			if (uids.Count == 0)
				throw new ArgumentException ("Cannot search for an empty set of unique identifiers.", nameof (uids));

			Uids = uids;
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="T:MailKit.Search.UidSearchQuery"/> class.
		/// </summary>
		/// <remarks>
		/// Creates a new unique identifier-based search query.
		/// </remarks>
		/// <param name="uid">The unique identifier to match against.</param>
		/// <exception cref="System.ArgumentException">
		/// <paramref name="uid"/> is an invalid unique identifier.
		/// </exception>
		public UidSearchQuery (UniqueId uid) : base (SearchTerm.Uid)
		{
			if (!uid.IsValid)
				throw new ArgumentException ("Cannot search for an invalid unique identifier.", nameof (uid));

View on GitHub (pinned to 9d3859a785)