---
title: "Remove UTM parameters from a URL in C#"
url: https://recursiveai.org/onecontact/cookbook/strip-tracking-parameters-from-url/
description: "Remove UTM parameters from a URL in C# with OneContact. Keep query parameters such as product IDs, and see what happens to fragments."
version: "1.0.0"
---

# Remove UTM parameters from a URL in C#

**OneContact** is a free, open-source contact data library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies, no network access.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneContact) · [GitHub](https://github.com/recursive-ai-oss/onecontact-csharp) · [Docs](https://recursiveai.org/onecontact/docs/) · [Download example (.zip)](https://recursiveai.org/onecontact/cookbook/strip-tracking-parameters-from-url/strip-tracking-parameters-from-url.zip)

```shell
dotnet add package RecursiveAI.OneContact
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using RecursiveAI.OneContact;

var url = WebAddress.Parse(
    "https://example.com/?id=42"
    + "&utm_source=news");
Console.WriteLine(url.Canonical);
```

The URL keeps `id=42`:

```text
https://example.com/?id=42
```

`Canonical` also drops any `#fragment` and normalizes the host.

Unzip [the example](https://recursiveai.org/onecontact/cookbook/strip-tracking-parameters-from-url/strip-tracking-parameters-from-url.zip), then `dotnet run`.

## Full program

```csharp
using RecursiveAI.OneContact;

var url = WebAddress.Parse(
    "https://example.com/?id=42&UTM_SOURCE=mail"
    + "&tag=a&tag=b#details");
Console.WriteLine(url.Canonical);
Console.WriteLine(url.Flags);
```

`dotnet run` prints:

```text
https://example.com/?id=42&tag=a&tag=b
TrackingParametersRemoved, FragmentRemoved
```

If the URL can’t be parsed, `Parse` throws `ContactParseException`; use `TryParse` and check `IsWebsite` when handling user-entered HTTP or HTTPS links.

## Variations

When all query parameters are removed, OneContact also removes the `?`:

```csharp
using RecursiveAI.OneContact;

var url = WebAddress.Parse(
    "https://example.com/?utm_source=mail");
Console.WriteLine(url.Canonical);
```

```text
https://example.com/
```

## Related recipes

- [Parse a URL in C#](https://recursiveai.org/onecontact/cookbook/parse-url/)
- [Validate a URL in C#](https://recursiveai.org/onecontact/cookbook/validate-url/)

## The short answer

Parse the URL with `WebAddress.Parse` and read `Canonical`. It removes known tracking parameters such as `utm_source`, `utm_medium`, and `utm_campaign`, while keeping parameters like `id=42`. The full program also shows that both `tag` entries survive. Don’t use `Key` as the cleaned URL: that property is for comparing addresses, not for sharing them.

## What are UTM parameters used for?

UTM parameters are campaign labels carried in the query string. For example, `utm_source=news` can tell an analytics system that a visit came from a newsletter. [Google’s campaign URL guide](https://support.google.com/analytics/answer/10917952) describes the labels and what they measure. By contrast, `id=42` might tell the site which item to show. Erasing the whole query string would remove both.

OneContact matches names from a [documented list](https://recursiveai.org/onecontact/docs/web/#tracking-parameters), not every name beginning with `utm_`. The match ignores ASCII case, so `UTM_SOURCE` is removed too. The parser checks the spelling as written, without percent-decoding it: `%75tm_source` and a custom name such as `utm_custom` both stay. The list also includes known click identifiers such as `gclid` and `fbclid`.

## How do I remove UTM parameters from a share link?

Use `Canonical` when you want the normalized address and don’t need to keep a section anchor. It drops the fragment, such as `#details`, even if no tracking parameters were removed. It also normalizes the host and omits a default port. This is not a query-only edit. If the link needs to scroll to a specific section, keep the original text; the parsed value doesn’t store the fragment. See the [URL and fragment behavior](https://recursiveai.org/onecontact/docs/api-shapes/) before replacing a saved link.

A signed URL may depend on its exact query or serialization. Keep those links as supplied rather than passing them through canonicalization. Likewise, removing known tags doesn’t remove identifiers in the path or under names outside the list, and it doesn’t prevent the destination from tracking a visit.
