---
name: kimaai_chatbot_session_retention_before_cleanup
type: action
since: 1.4.9
tier: free
feature: chat-history
area: chatbot
summary: Fires before a run of the cleanup deletes old chatbot conversations.
parameters:
  - name: context
    type: array
    description: source (schedule or fallback), settings, mode, effectiveDays and cutoffTimestamp, the date and time ("Y-m-d H:i:s", site time zone) before which conversations count as old.
---

# kimaai_chatbot_session_retention_before_cleanup

## When it fires

When a run of the conversation cleanup goes ahead: after `kimaai_chatbot_session_retention_should_run` let it and the run took its lock, before the first batch is deleted. The run then deletes the conversations whose last message is older than `cutoffTimestamp`, batch by batch, until none is left or a batch fails.

The action is the place to keep a copy of what is about to go, for example in an archive of your own: read the conversations whose last message is older than `$context['cutoffTimestamp']`. It cannot stop the run; use `kimaai_chatbot_session_retention_should_run` for that.

## Example

```php
add_action(
  'kimaai_chatbot_session_retention_before_cleanup',
  function (array $context): void {
    error_log(sprintf('KimaAI deletes the conversations older than %s (%s)', $context['cutoffTimestamp'], $context['source']));
  }
);
```

## Related

- `kimaai_chatbot_session_retention_after_cleanup`: fires after the run with what it deleted.
- `kimaai_chatbot_session_retention_should_run`: pauses a run.
