URLSearchParams: delete() method
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
Note: This feature is available in Web Workers.
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.
Syntax
delete(name)
delete(name, value)
Parameters
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.
const url = new URL("https://example.com?foo=1&bar=2&foo=3");
const params = new URLSearchParams(url.search);
console.log(`Query string (before):\t ${params}`);
params.delete("foo");
console.log(`Query string (after):\t ${params}`);
The log below shows that all parameters that have the name of foo
are deleted.
Query string (before): foo=1&bar=2&foo=3 Query string (after): bar=2
Delete parameters with specified name and value
This example shows how to delete query parameters that match a particular name and value.
const url = new URL("https://example.com?foo=1&bar=2&foo=3&foo=1");
const params = new URLSearchParams(url.search);
console.log(`Query string (before):\t ${params}`);
params.delete("foo", "1");
console.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).
Query string (before): foo=1&bar=2&foo=3&foo=1 Query string (after): bar=2&foo=3
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
Specification |
---|
URL Standard # dom-urlsearchparams-delete |
Browser compatibility
BCD tables only load in the browser