diff --git a/src/api/controllers/instance.controller.ts b/src/api/controllers/instance.controller.ts index 6a6910688..5379d4f80 100644 --- a/src/api/controllers/instance.controller.ts +++ b/src/api/controllers/instance.controller.ts @@ -456,7 +456,21 @@ export class InstanceController { if (this.configService.get('CHATWOOT').ENABLED) waInstances?.clearCacheChatwoot(); if (instance.state === 'connecting' || instance.state === 'open') { - await this.logout({ instanceName }); + try { + await this.logout({ instanceName }); + } catch (error) { + // logout can throw "Connection Closed" when the underlying Baileys + // socket is already dead but waInstances[name] still exists. We + // must continue to the remove.instance emit below — that is the + // only path that purges the in-memory entry and runs cleaningUp(). + // Without this catch, the stale entry persists until the entire + // process restarts. + this.logger.warn({ + message: 'logout failed during deleteInstance — proceeding with cleanup', + instanceName, + error, + }); + } } try {