W3cubDocs

/Web APIs

URLSearchParams: delete() method

The delete() method of the URLSearchParams interface deletes specified parameters and their associated value(s) from the list of all search parameters.

A parameter name and optional value are used to match parameters. If only a parameter name is specified, then all search parameters that match the name are deleted, along with their associated values. If both a parameter name and value are specified, then all search parameters that match both the parameter name and value are deleted.

Note: This feature is available in Web Workers

Syntax

js

delete(name)
delete(name, value)

Parameters

name

The name of the parameters to be deleted.

value Optional

The value that parameters must match, along with the given name, to be deleted.

Return value

None (undefined).

Examples

Delete all parameters with specified name

This example shows how to delete all query parameters (and values) that have a particular name.

js

const url = new URL("https://example.com?foo=1&bar=2&foo=3");
const params = new URLSearchParams(url.search);
log(`Query string (before):\t ${params}`);
params.delete("foo");
log(`Query string (after):\t ${params}`);

The log below shows that all parameters that have the name of foo are deleted.

Delete parameters with specified name and value

This example shows how to delete query parameters that match a particular name and value.

js

const url = new URL("https://example.com?foo=1&bar=2&foo=3&foo=1");
const params = new URLSearchParams(url.search);
log(`Query string (before):\t ${params}`);
params.delete("foo", "1");
log(`Query string (after):\t ${params}`);

All parameters that match both the parameter name and value should be deleted (there is no reason to specify two parameters with the same name and value as shown above).

If your browser supports the value option, the "after" string should be bar=2&foo=3. Otherwise the result will be the same as in the previous example (bar=2).

Specifications

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
delete 49 17 29 No 36 14
10.1Removing a non-existent query parameter doesn't remove ? from the URL. See bug 193022.
49 49 29 36 14
10.3Removing a non-existent query parameter doesn't remove ? from the URL. See bug 193022.
5.0
value_parameter No No 115 No No preview No No 115 No No No

See also

© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams/delete