diff --git a/packages/cli/src/WorkflowExecuteAdditionalData.ts b/packages/cli/src/WorkflowExecuteAdditionalData.ts index cf21a80ca4..4488af126c 100644 --- a/packages/cli/src/WorkflowExecuteAdditionalData.ts +++ b/packages/cli/src/WorkflowExecuteAdditionalData.ts @@ -44,7 +44,7 @@ import { import pick from 'lodash.pick'; import type { FindOptionsWhere } from 'typeorm'; -import { LessThanOrEqual } from 'typeorm'; +import { LessThanOrEqual, In } from 'typeorm'; import { DateUtils } from 'typeorm/util/DateUtils'; import config from '@/config'; import * as Db from '@/Db'; @@ -212,7 +212,9 @@ async function pruneExecutionData(this: WorkflowHooks): Promise { // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment const utcDate = DateUtils.mixedDateToUtcDatetimeString(date); - const toPrune: FindOptionsWhere = { stoppedAt: LessThanOrEqual(utcDate) }; + const toPrune: Array> = [ + { stoppedAt: LessThanOrEqual(utcDate) }, + ]; if (maxCount > 0) { const executions = await Db.collections.Execution.find({ @@ -223,27 +225,29 @@ async function pruneExecutionData(this: WorkflowHooks): Promise { }); if (executions[0]) { - toPrune.id = LessThanOrEqual(executions[0].id); + toPrune.push({ id: LessThanOrEqual(executions[0].id) }); } } const isBinaryModeDefaultMode = config.getEnv('binaryDataManager.mode') === 'default'; try { - const executions = isBinaryModeDefaultMode - ? [] - : await Db.collections.Execution.find({ - select: ['id'], - where: toPrune, - }); - await Db.collections.Execution.delete(toPrune); setTimeout(() => { throttling = false; }, timeout * 1000); - // Mark binary data for deletion for all executions - if (!isBinaryModeDefaultMode) - await BinaryDataManager.getInstance().markDataForDeletionByExecutionIds( - executions.map(({ id }) => id), - ); + let executionIds: Array; + do { + executionIds = ( + await Db.collections.Execution.find({ + select: ['id'], + where: toPrune, + take: 100, + }) + ).map(({ id }) => id); + await Db.collections.Execution.delete({ id: In(executionIds) }); + // Mark binary data for deletion for all executions + if (!isBinaryModeDefaultMode) + await BinaryDataManager.getInstance().markDataForDeletionByExecutionIds(executionIds); + } while (executionIds.length > 0); } catch (error) { ErrorReporter.error(error); throttling = false;