Skip to content

[Synthetics] Perform clean up delete in batches#245775

Merged
shahzad31 merged 4 commits intoelastic:mainfrom
shahzad31:optimise-clean-up
Dec 10, 2025
Merged

[Synthetics] Perform clean up delete in batches#245775
shahzad31 merged 4 commits intoelastic:mainfrom
shahzad31:optimise-clean-up

Conversation

@shahzad31
Copy link
Copy Markdown
Contributor

@shahzad31 shahzad31 commented Dec 10, 2025

Summary

Refactor code to Perform clean up delete in batches, unit tests have been added for the changes.

Instead of calling on all found package policies to delete, do a for loop in batches

 await fleet.packagePolicyService.delete(soClient, esClient, batch, {
      force: true,
      spaceIds: ['*'],
    });

@github-actions github-actions bot added the author:actionable-obs PRs authored by the actionable obs team label Dec 10, 2025
@shahzad31 shahzad31 marked this pull request as ready for review December 10, 2025 09:35
@shahzad31 shahzad31 requested a review from a team as a code owner December 10, 2025 09:35
@shahzad31 shahzad31 added release_note:skip Skip the PR/issue when compiling release notes backport:version Backport to applied version labels v9.2.3 v9.1.9 v8.19.9 and removed v9.1.9 labels Dec 10, 2025
@shahzad31 shahzad31 requested a review from Copilot December 10, 2025 09:43
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors the duplicate package policy cleanup logic to delete policies in batches of 100 instead of attempting to delete all at once, preventing potential issues with oversized payloads.

Key changes:

  • Extracted deletion logic into a new deleteDuplicatePackagePolicies function that processes deletions in batches of 100
  • Added comprehensive unit tests covering empty arrays, small single-batch deletions, and large multi-batch scenarios
  • Enhanced logging to show batch progress during deletion

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.

File Description
x-pack/solutions/observability/plugins/synthetics/server/tasks/clean_up_duplicate_policies.ts Refactored deletion logic into a new exported function with batching logic; added ElasticsearchClient type import
x-pack/solutions/observability/plugins/synthetics/server/tasks/clean_up_duplicate_policies.test.ts Added new test file with three test cases covering empty array, single batch, and multi-batch deletion scenarios

@elasticmachine
Copy link
Copy Markdown
Contributor

Pinging @elastic/obs-ux-management-team (Team:obs-ux-management)

@shahzad31 shahzad31 enabled auto-merge (squash) December 10, 2025 17:37
@shahzad31 shahzad31 merged commit 287b929 into elastic:main Dec 10, 2025
12 checks passed
@kibanamachine
Copy link
Copy Markdown
Contributor

Starting backport for target branches: 8.19, 9.2

https://github.com/elastic/kibana/actions/runs/20109525028

@elasticmachine
Copy link
Copy Markdown
Contributor

💚 Build Succeeded

Metrics [docs]

✅ unchanged

History

kibanamachine pushed a commit to kibanamachine/kibana that referenced this pull request Dec 10, 2025
## Summary

Refactor code to Perform clean up delete in batches, unit tests have
been added for the changes.

Instead of calling on all found package policies to delete, do a for
loop in batches
```
 await fleet.packagePolicyService.delete(soClient, esClient, batch, {
      force: true,
      spaceIds: ['*'],
    });
```

(cherry picked from commit 287b929)
@kibanamachine
Copy link
Copy Markdown
Contributor

💔 Some backports could not be created

Status Branch Result
8.19 Backport failed because of merge conflicts
9.2

Note: Successful backport PRs will be merged automatically after passing CI.

Manual backport

To create the backport manually run:

node scripts/backport --pr 245775

Questions ?

Please refer to the Backport tool documentation

kibanamachine added a commit that referenced this pull request Dec 10, 2025
)

# Backport

This will backport the following commits from `main` to `9.2`:
- [[Synthetics] Perform clean up delete in batches
(#245775)](#245775)

<!--- Backport version: 9.6.6 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sorenlouv/backport)

<!--BACKPORT
[{"author":{"name":"Shahzad","email":"shahzad31comp@gmail.com"},"sourceCommit":{"committedDate":"2025-12-10T18:40:46Z","message":"[Synthetics]
Perform clean up delete in batches (#245775)\n\n## Summary\n\nRefactor
code to Perform clean up delete in batches, unit tests have\nbeen added
for the changes.\n\nInstead of calling on all found package policies to
delete, do a for\nloop in batches\n```\n await
fleet.packagePolicyService.delete(soClient, esClient, batch, {\n force:
true,\n spaceIds: ['*'],\n
});\n```","sha":"287b929c42199a35c23fece3300d1eddc89bb913","branchLabelMapping":{"^v9.3.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","backport:version","v9.3.0","author:actionable-obs","Team:obs-ux-management","v9.2.3","v8.19.9"],"title":"[Synthetics]
Perform clean up delete in
batches","number":245775,"url":"https://github.com/elastic/kibana/pull/245775","mergeCommit":{"message":"[Synthetics]
Perform clean up delete in batches (#245775)\n\n## Summary\n\nRefactor
code to Perform clean up delete in batches, unit tests have\nbeen added
for the changes.\n\nInstead of calling on all found package policies to
delete, do a for\nloop in batches\n```\n await
fleet.packagePolicyService.delete(soClient, esClient, batch, {\n force:
true,\n spaceIds: ['*'],\n
});\n```","sha":"287b929c42199a35c23fece3300d1eddc89bb913"}},"sourceBranch":"main","suggestedTargetBranches":["9.2","8.19"],"targetPullRequestStates":[{"branch":"main","label":"v9.3.0","branchLabelMappingKey":"^v9.3.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/245775","number":245775,"mergeCommit":{"message":"[Synthetics]
Perform clean up delete in batches (#245775)\n\n## Summary\n\nRefactor
code to Perform clean up delete in batches, unit tests have\nbeen added
for the changes.\n\nInstead of calling on all found package policies to
delete, do a for\nloop in batches\n```\n await
fleet.packagePolicyService.delete(soClient, esClient, batch, {\n force:
true,\n spaceIds: ['*'],\n
});\n```","sha":"287b929c42199a35c23fece3300d1eddc89bb913"}},{"branch":"9.2","label":"v9.2.3","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"8.19","label":"v8.19.9","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Shahzad <shahzad31comp@gmail.com>
shahzad31 added a commit to shahzad31/kibana that referenced this pull request Dec 10, 2025
## Summary

Refactor code to Perform clean up delete in batches, unit tests have
been added for the changes.

Instead of calling on all found package policies to delete, do a for
loop in batches
```
 await fleet.packagePolicyService.delete(soClient, esClient, batch, {
      force: true,
      spaceIds: ['*'],
    });
```

(cherry picked from commit 287b929)

# Conflicts:
#	x-pack/solutions/observability/plugins/synthetics/server/tasks/clean_up_duplicate_policies.ts
@shahzad31
Copy link
Copy Markdown
Contributor Author

💚 All backports created successfully

Status Branch Result
8.19

Note: Successful backport PRs will be merged automatically after passing CI.

Questions ?

Please refer to the Backport tool documentation

@shahzad31 shahzad31 deleted the optimise-clean-up branch December 10, 2025 22:17
shahzad31 added a commit that referenced this pull request Dec 11, 2025
…5926)

# Backport

This will backport the following commits from `main` to `8.19`:
- [[Synthetics] Perform clean up delete in batches
(#245775)](#245775)

<!--- Backport version: 10.2.0 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sorenlouv/backport)

<!--BACKPORT
[{"author":{"name":"Shahzad","email":"shahzad31comp@gmail.com"},"sourceCommit":{"committedDate":"2025-12-10T18:40:46Z","message":"[Synthetics]
Perform clean up delete in batches (#245775)\n\n## Summary\n\nRefactor
code to Perform clean up delete in batches, unit tests have\nbeen added
for the changes.\n\nInstead of calling on all found package policies to
delete, do a for\nloop in batches\n```\n await
fleet.packagePolicyService.delete(soClient, esClient, batch, {\n force:
true,\n spaceIds: ['*'],\n
});\n```","sha":"287b929c42199a35c23fece3300d1eddc89bb913","branchLabelMapping":{"^v9.3.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","backport:version","v9.3.0","author:actionable-obs","Team:obs-ux-management","v9.2.3","v8.19.9"],"title":"[Synthetics]
Perform clean up delete in
batches","number":245775,"url":"https://github.com/elastic/kibana/pull/245775","mergeCommit":{"message":"[Synthetics]
Perform clean up delete in batches (#245775)\n\n## Summary\n\nRefactor
code to Perform clean up delete in batches, unit tests have\nbeen added
for the changes.\n\nInstead of calling on all found package policies to
delete, do a for\nloop in batches\n```\n await
fleet.packagePolicyService.delete(soClient, esClient, batch, {\n force:
true,\n spaceIds: ['*'],\n
});\n```","sha":"287b929c42199a35c23fece3300d1eddc89bb913"}},"sourceBranch":"main","suggestedTargetBranches":["8.19"],"targetPullRequestStates":[{"branch":"main","label":"v9.3.0","branchLabelMappingKey":"^v9.3.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/245775","number":245775,"mergeCommit":{"message":"[Synthetics]
Perform clean up delete in batches (#245775)\n\n## Summary\n\nRefactor
code to Perform clean up delete in batches, unit tests have\nbeen added
for the changes.\n\nInstead of calling on all found package policies to
delete, do a for\nloop in batches\n```\n await
fleet.packagePolicyService.delete(soClient, esClient, batch, {\n force:
true,\n spaceIds: ['*'],\n
});\n```","sha":"287b929c42199a35c23fece3300d1eddc89bb913"}},{"branch":"9.2","label":"v9.2.3","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"url":"https://github.com/elastic/kibana/pull/245904","number":245904,"state":"MERGED","mergeCommit":{"sha":"03c026c2876802f5fe175db71bd791b4fe589abe","message":"[9.2]
[Synthetics] Perform clean up delete in batches (#245775) (#245904)\n\n#
Backport\n\nThis will backport the following commits from `main` to
`9.2`:\n- [[Synthetics] Perform clean up delete in
batches\n(#245775)](https://github.com/elastic/kibana/pull/245775)\n\n\n\n###
Questions ?\nPlease refer to the [Backport
tool\ndocumentation](https://github.com/sorenlouv/backport)\n\n\n\nCo-authored-by:
Shahzad
<shahzad31comp@gmail.com>"}},{"branch":"8.19","label":"v8.19.9","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->
seanrathier pushed a commit to seanrathier/kibana that referenced this pull request Dec 15, 2025
## Summary

Refactor code to Perform clean up delete in batches, unit tests have
been added for the changes.

Instead of calling on all found package policies to delete, do a for
loop in batches
```
 await fleet.packagePolicyService.delete(soClient, esClient, batch, {
      force: true,
      spaceIds: ['*'],
    });
```
shahzad31 added a commit to shahzad31/kibana that referenced this pull request Jan 9, 2026
## Summary

Refactor code to Perform clean up delete in batches, unit tests have
been added for the changes.

Instead of calling on all found package policies to delete, do a for
loop in batches
```
 await fleet.packagePolicyService.delete(soClient, esClient, batch, {
      force: true,
      spaceIds: ['*'],
    });
```

(cherry picked from commit 287b929)
@shahzad31
Copy link
Copy Markdown
Contributor Author

💚 All backports created successfully

Status Branch Result
9.1

Note: Successful backport PRs will be merged automatically after passing CI.

Questions ?

Please refer to the Backport tool documentation

@kibanamachine kibanamachine added the backport missing Added to PRs automatically when the are determined to be missing a backport. label Jan 12, 2026
@kibanamachine
Copy link
Copy Markdown
Contributor

Looks like this PR has backport PRs but they still haven't been merged. Please merge them ASAP to keep the branches relatively in sync.
cc: @shahzad31

shahzad31 added a commit that referenced this pull request Jan 12, 2026
)

# Backport

This will backport the following commits from `main` to `9.1`:
- [[Synthetics] Perform clean up delete in batches
(#245775)](#245775)

<!--- Backport version: 10.2.0 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sorenlouv/backport)

<!--BACKPORT
[{"author":{"name":"Shahzad","email":"shahzad31comp@gmail.com"},"sourceCommit":{"committedDate":"2025-12-10T18:40:46Z","message":"[Synthetics]
Perform clean up delete in batches (#245775)\n\n## Summary\n\nRefactor
code to Perform clean up delete in batches, unit tests have\nbeen added
for the changes.\n\nInstead of calling on all found package policies to
delete, do a for\nloop in batches\n```\n await
fleet.packagePolicyService.delete(soClient, esClient, batch, {\n force:
true,\n spaceIds: ['*'],\n
});\n```","sha":"287b929c42199a35c23fece3300d1eddc89bb913","branchLabelMapping":{"^v9.3.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","backport:version","v9.3.0","author:actionable-obs","Team:obs-ux-management","v9.2.3","v8.19.9","v9.1.10"],"title":"[Synthetics]
Perform clean up delete in
batches","number":245775,"url":"https://github.com/elastic/kibana/pull/245775","mergeCommit":{"message":"[Synthetics]
Perform clean up delete in batches (#245775)\n\n## Summary\n\nRefactor
code to Perform clean up delete in batches, unit tests have\nbeen added
for the changes.\n\nInstead of calling on all found package policies to
delete, do a for\nloop in batches\n```\n await
fleet.packagePolicyService.delete(soClient, esClient, batch, {\n force:
true,\n spaceIds: ['*'],\n
});\n```","sha":"287b929c42199a35c23fece3300d1eddc89bb913"}},"sourceBranch":"main","suggestedTargetBranches":["9.1"],"targetPullRequestStates":[{"branch":"main","label":"v9.3.0","branchLabelMappingKey":"^v9.3.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/245775","number":245775,"mergeCommit":{"message":"[Synthetics]
Perform clean up delete in batches (#245775)\n\n## Summary\n\nRefactor
code to Perform clean up delete in batches, unit tests have\nbeen added
for the changes.\n\nInstead of calling on all found package policies to
delete, do a for\nloop in batches\n```\n await
fleet.packagePolicyService.delete(soClient, esClient, batch, {\n force:
true,\n spaceIds: ['*'],\n
});\n```","sha":"287b929c42199a35c23fece3300d1eddc89bb913"}},{"branch":"9.2","label":"v9.2.3","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"url":"https://github.com/elastic/kibana/pull/245904","number":245904,"state":"MERGED","mergeCommit":{"sha":"03c026c2876802f5fe175db71bd791b4fe589abe","message":"[9.2]
[Synthetics] Perform clean up delete in batches (#245775) (#245904)\n\n#
Backport\n\nThis will backport the following commits from `main` to
`9.2`:\n- [[Synthetics] Perform clean up delete in
batches\n(#245775)](https://github.com/elastic/kibana/pull/245775)\n\n\n\n###
Questions ?\nPlease refer to the [Backport
tool\ndocumentation](https://github.com/sorenlouv/backport)\n\n\n\nCo-authored-by:
Shahzad
<shahzad31comp@gmail.com>"}},{"branch":"8.19","label":"v8.19.9","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"url":"https://github.com/elastic/kibana/pull/245926","number":245926,"state":"MERGED","mergeCommit":{"sha":"e8062fff81c26b6be11c5ba2cdb27169821fee78","message":"[8.19]
[Synthetics] Perform clean up delete in batches (#245775) (#245926)\n\n#
Backport\n\nThis will backport the following commits from `main` to
`8.19`:\n- [[Synthetics] Perform clean up delete in
batches\n(#245775)](https://github.com/elastic/kibana/pull/245775)\n\n\n\n###
Questions ?\nPlease refer to the [Backport
tool\ndocumentation](https://github.com/sorenlouv/backport)\n\n"}},{"branch":"9.1","label":"v9.1.10","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->
@kibanamachine kibanamachine removed the backport missing Added to PRs automatically when the are determined to be missing a backport. label Jan 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author:actionable-obs PRs authored by the actionable obs team backport:version Backport to applied version labels release_note:skip Skip the PR/issue when compiling release notes Team:obs-ux-management v8.19.9 v9.1.10 v9.2.3 v9.3.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants