diff --git a/server/queues/tasks/ExportTask.ts b/server/queues/tasks/ExportTask.ts index 159168bed..906b2d15b 100644 --- a/server/queues/tasks/ExportTask.ts +++ b/server/queues/tasks/ExportTask.ts @@ -40,6 +40,8 @@ export default abstract class ExportTask extends BaseTask { }, }); + let filePath: string | undefined; + try { Logger.info("task", `ExportTask processing data for ${fileOperationId}`, { includeAttachments: fileOperation.includeAttachments, @@ -49,7 +51,7 @@ export default abstract class ExportTask extends BaseTask { state: FileOperationState.Creating, }); - const filePath = await this.export(collections, fileOperation); + filePath = await this.export(collections, fileOperation); Logger.info("task", `ExportTask uploading data for ${fileOperationId}`); @@ -95,6 +97,12 @@ export default abstract class ExportTask extends BaseTask { }).schedule(); } throw error; + } finally { + if (filePath) { + void fs.promises.unlink(filePath).catch((error) => { + Logger.error(`Failed to delete temporary file ${filePath}`, error); + }); + } } }