diff --git a/x-pack/platform/plugins/shared/fleet/server/services/epm/elasticsearch/template/remove_legacy.ts b/x-pack/platform/plugins/shared/fleet/server/services/epm/elasticsearch/template/remove_legacy.ts index d90cf541ce69d..155731b73dfaa 100644 --- a/x-pack/platform/plugins/shared/fleet/server/services/epm/elasticsearch/template/remove_legacy.ts +++ b/x-pack/platform/plugins/shared/fleet/server/services/epm/elasticsearch/template/remove_legacy.ts @@ -11,6 +11,7 @@ import type { } from '@elastic/elasticsearch/lib/api/types'; import type { ElasticsearchClient, Logger } from '@kbn/core/server'; +import { USER_SETTINGS_TEMPLATE_SUFFIX } from '../../../../../common/constants'; import type { InstallablePackage, RegistryDataStream } from '../../../../types'; import { getRegistryDataStreamAssetBaseName } from '../../../../../common/services'; const LEGACY_TEMPLATE_SUFFIXES = ['@mappings', '@settings']; @@ -108,6 +109,9 @@ export const _filterComponentTemplatesInUse = ({ const usedByLookup = _getIndexTemplatesToUsedByMap(indexTemplates); return componentTemplateNames.filter((componentTemplateName) => { + if (componentTemplateName.endsWith(USER_SETTINGS_TEMPLATE_SUFFIX)) { + return false; + } const indexTemplatesUsingComponentTemplate = usedByLookup.get(componentTemplateName); if (indexTemplatesUsingComponentTemplate?.length) { diff --git a/x-pack/platform/plugins/shared/fleet/server/services/epm/packages/remove.test.ts b/x-pack/platform/plugins/shared/fleet/server/services/epm/packages/remove.test.ts index 2886774a32d12..57238bb6b5da3 100644 --- a/x-pack/platform/plugins/shared/fleet/server/services/epm/packages/remove.test.ts +++ b/x-pack/platform/plugins/shared/fleet/server/services/epm/packages/remove.test.ts @@ -4,13 +4,14 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ +import { elasticsearchServiceMock } from '@kbn/core/server/mocks'; -import { PACKAGES_SAVED_OBJECT_TYPE } from '../../../../common'; +import { ElasticsearchAssetType, PACKAGES_SAVED_OBJECT_TYPE } from '../../../../common'; import { packagePolicyService } from '../..'; import { auditLoggingService } from '../../audit_logging'; -import { removeInstallation } from './remove'; +import { deleteESAssets, removeInstallation } from './remove'; jest.mock('../..', () => { return { @@ -111,3 +112,38 @@ describe('removeInstallation', () => { }); }); }); + +describe('deleteESAssets', () => { + it('should not delete @custom components template', async () => { + const esClient = elasticsearchServiceMock.createInternalClient(); + await deleteESAssets( + [ + { + id: 'logs@custom', + type: ElasticsearchAssetType.componentTemplate, + }, + ], + esClient + ); + + expect(esClient.cluster.deleteComponentTemplate).not.toBeCalled(); + }); + + it('should delete @package components template', async () => { + const esClient = elasticsearchServiceMock.createInternalClient(); + await deleteESAssets( + [ + { + id: 'logs-nginx.access@package', + type: ElasticsearchAssetType.componentTemplate, + }, + ], + esClient + ); + + expect(esClient.cluster.deleteComponentTemplate).toBeCalledWith( + { name: 'logs-nginx.access@package' }, + expect.anything() + ); + }); +}); diff --git a/x-pack/platform/plugins/shared/fleet/server/services/epm/packages/remove.ts b/x-pack/platform/plugins/shared/fleet/server/services/epm/packages/remove.ts index 3892eaa951e5f..9ae2c90e969d3 100644 --- a/x-pack/platform/plugins/shared/fleet/server/services/epm/packages/remove.ts +++ b/x-pack/platform/plugins/shared/fleet/server/services/epm/packages/remove.ts @@ -24,6 +24,7 @@ import { PACKAGE_POLICY_SAVED_OBJECT_TYPE, PACKAGES_SAVED_OBJECT_TYPE, SO_SEARCH_LIMIT, + USER_SETTINGS_TEMPLATE_SUFFIX, } from '../../../constants'; import { ElasticsearchAssetType } from '../../../types'; import type { @@ -368,7 +369,7 @@ async function deleteIndexTemplate(esClient: ElasticsearchClient, name: string): async function deleteComponentTemplate(esClient: ElasticsearchClient, name: string): Promise { // '*' shouldn't ever appear here, but it still would delete all templates - if (name && name !== '*') { + if (name && name !== '*' && !name.endsWith(USER_SETTINGS_TEMPLATE_SUFFIX)) { try { await esClient.cluster.deleteComponentTemplate({ name }, { ignore: [404] }); } catch (error) {