diff --git a/x-pack/plugins/fleet/server/services/agent_policy.test.ts b/x-pack/plugins/fleet/server/services/agent_policy.test.ts index d760b9920fad4..277ca1f05b807 100644 --- a/x-pack/plugins/fleet/server/services/agent_policy.test.ts +++ b/x-pack/plugins/fleet/server/services/agent_policy.test.ts @@ -24,7 +24,7 @@ import type { } from '../types'; import { AGENT_POLICY_SAVED_OBJECT_TYPE } from '../constants'; -import { AGENT_POLICY_INDEX } from '../../common'; +import { AGENT_POLICY_INDEX, SO_SEARCH_LIMIT } from '../../common'; import { agentPolicyService } from './agent_policy'; import { agentPolicyUpdateEventHandler } from './agent_policy_update'; @@ -1006,5 +1006,20 @@ describe('agent policy', () => { message: 'User deleting policy [id=test-agent-policy]', }); }); + + it('should call deleteByQuery multiple time if there is more than 10000 .fleet-policies', async () => { + const esClient = elasticsearchServiceMock.createClusterClient().asInternalUser; + + esClient.deleteByQuery.mockResolvedValueOnce({ + deleted: SO_SEARCH_LIMIT, + }); + esClient.deleteByQuery.mockResolvedValueOnce({ + deleted: 10, + }); + + await agentPolicyService.deleteFleetServerPoliciesForPolicyId(esClient, 'test-agent-policy'); + + expect(esClient.deleteByQuery).toBeCalledTimes(2); + }); }); }); diff --git a/x-pack/plugins/fleet/server/services/agent_policy.ts b/x-pack/plugins/fleet/server/services/agent_policy.ts index 57c876fe1441a..8c521d88819d6 100644 --- a/x-pack/plugins/fleet/server/services/agent_policy.ts +++ b/x-pack/plugins/fleet/server/services/agent_policy.ts @@ -1033,17 +1033,21 @@ class AgentPolicyService { message: `User deleting policy [id=${agentPolicyId}]`, }); - await esClient.deleteByQuery({ - index: AGENT_POLICY_INDEX, - ignore_unavailable: true, - body: { + let hasMore = true; + while (hasMore) { + const res = await esClient.deleteByQuery({ + index: AGENT_POLICY_INDEX, + ignore_unavailable: true, + scroll_size: SO_SEARCH_LIMIT, + refresh: true, query: { term: { policy_id: agentPolicyId, }, }, - }, - }); + }); + hasMore = (res.deleted ?? 0) === SO_SEARCH_LIMIT; + } } public async getLatestFleetPolicy(esClient: ElasticsearchClient, agentPolicyId: string) {