• Node.js Video Tutorials

NodeJS - URLSearchParams.delete() Method



The NodeJS urlSearchParams.delete() method of class URLSearchParams accepts a name as a parameter and removes all its name/value pairs whose name is the name from the query string.

The query portion of the URL can be read and written using the URLSearchParams API. This class is also available on the global object.

Syntax

Following is the syntax of the NodeJS URLSearchParams.delete() method

URLSearchParams.delete(name)

Parameters

  • name: This parameter specifies the name to be deleted.

Return Value

This method removes all the name/value pairs whose name is name.

Example

If we pass a name to the NodeJS urlSearchParams.delete() method, it will remove that name/value pair from the input query string.

In the following example, we are trying to delete a name/value pair from the query string by passing the name to the NodeJS delete() method.

const url = require('node:url');

let Myurl = new URL('https://www.tutorialspoint.com?AWS=1499&DEVOPS=1999&Cloud=2499');
console.log("URL: ", Myurl.href); If we pass a key/value pair to the append() method, it will add them at the end of the query string.

let params = new URLSearchParams('AWS=1499&DEVOPS=1999&Cloud=2499');
console.log("Query portion of the URL: " + params.toString());

//deletes 'DEVOPS' from the query string.
params.delete('DEVOPS');
//The final Query string is: 'AWS=1499&Cloud=2499'
console.log("After deleting 'DEVOPS': " + params.toString());

Output

As we can see in the output below, the name/value pairs which are having the name 'DEVOPS' are deleted.

URL:  https://www.tutorialspoint.com/?AWS=1499&DEVOPS=1999&Cloud=2499
Query portion of the URL: AWS=1499&DEVOPS=1999&Cloud=2499
After deleting 'DEVOPS': AWS=1499&Cloud=2499

Example

The delete() method will remove all the name/value pairs from the query string which are having the name to be deleted.

In the following example, we are trying to delete the name/value pairs whose name is 'DEVOPS'.

const url = require('node:url');

let Myurl = new URL('https://www.tutorialspoint.com?AWS=1499&DEVOPS=1999&Cloud=2499&DEVOPS=4999&DEVOPS=5999');
console.log("URL: ", Myurl.href);

let params = new URLSearchParams('AWS=1499&DEVOPS=1999&Cloud=2499&DEVOPS=4999&DEVOPS=5999');
console.log("Query portion of the URL: " + params.toString());

//deletes every pair with name 'DEVOPS' from the query string.
params.delete('DEVOPS');
//The final Query string is: 'AWS=1499&Cloud=2499'
console.log("After deleting 'DEVOPS': " + params.toString());

Output

As we can see in the output below, all the pair appearances of 'DEVOPS' are deleted from the query string.

URL:  https://www.tutorialspoint.com/?AWS=1499&DEVOPS=1999&Cloud=2499&DEVOPS=4999&DEVOPS=5999
Query portion of the URL: AWS=1499&DEVOPS=1999&Cloud=2499&DEVOPS=4999&DEVOPS=5999
After deleting 'DEVOPS': AWS=1499&Cloud=2499

Example

The query string will remain the same if the name to be deleted is not in the input query string.

In the following example, we are trying to delete the name which is not in the query string.

const url = require('node:url');

let Myurl = new URL('https://www.tutorialspoint.com?AWS=1499&DEVOPS=1999&Cloud=2499&DEVOPS=4999&DEVOPS=5999');
console.log("URL: ", Myurl.href);

let params = new URLSearchParams('AWS=1499&DEVOPS=1999&Cloud=2499&DEVOPS=4999&DEVOPS=5999');
console.log("Query portion of the URL: " + params.toString());

//deletes every pair with name 'GCP' from the query string.
params.delete('GCP');
//The final Query string is: 'AWS=1499&Cloud=2499'
console.log("After deleting 'GCP': " + params.toString());

Output

As we can see in the output, the input query string remains the same.

URL:  https://www.tutorialspoint.com/?AWS=1499&DEVOPS=1999&Cloud=2499&DEVOPS=4999&DEVOPS=5999
Query portion of the URL: AWS=1499&DEVOPS=1999&Cloud=2499&DEVOPS=4999&DEVOPS=5999
After deleting 'GCP': AWS=1499&DEVOPS=1999&Cloud=2499&DEVOPS=4999&DEVOPS=5999
nodejs_url_module.htm
Advertisements