Skip to content

delete searchParams during iteration will cause skipping #58717

Closed as not planned
@beenotung

Description

@beenotung

Version

v22.13.1

Platform

Linux BeenoTung-Archlinux-laptop-kudu 6.14.9-arch1-1 #1 SMP PREEMPT_DYNAMIC Thu, 29 May 2025 21:42:15 +0000 x86_64 GNU/Linux

Subsystem

No response

What steps will reproduce the bug?

When running below code in nodejs

let url = new URL('http://example.com?a=1&b=2&c=3&d=4')
let { searchParams } = url

console.log('all keys:', Array.from(searchParams.keys()))

for (let key of searchParams.keys()) {
  console.log('delete:', { key })
  searchParams.delete(key)
}

console.log('remains keys:', Array.from(searchParams.keys()))

How often does it reproduce? Is there a required condition?

The behavior can be reproduced every time, without additional condition.

What is the expected behavior? Why is that the expected behavior?

The expected behavior is to delete all params.

Workaround is to collect all elements into an array then iterate on the array, instead of iterate on the iterator directly, which is not intuitive IMO.

What do you see instead?

The observed output is listed as below

all keys: [ 'a', 'b', 'c', 'd' ]
delete: { key: 'a' }
delete: { key: 'c' }
remains keys: [ 'b', 'd' ]

Only the odd index keys are deleted.

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions