In Rails Action Mailbox, what is incineration, when does it delete an InboundEmail, and what must a mailbox do before then?
answer
- processed means delivered, failed or bounced
- incinerate_after defaults to 30 days
- IncinerationJob scheduled with wait:
- updated_at checked against end of day
- copy what you need into your models
basics
~10 sIncineration deletes processed InboundEmails and their raw messages after config.action_mailbox.incinerate_after, 30 days by default. A mailbox must copy everything the app needs, attachments included, into its own models before then.
solid answer
~40 sWhen an `InboundEmail`'s status changes to a processed state (`delivered`, `failed` or `bounced`), an `after_update_commit` callback calls `incinerate_later`, which enqueues `ActionMailbox::IncinerationJob` with `wait: ActionMailbox.incinerate_after`, 30 days unless configured (the Rails 8.1 default). When the job runs it destroys the record only if it is still processed and its `updated_at` is older than the end of the day `incinerate_after` ago; destroying it also purges the `raw_email` attachment through Active Storage. `config.action_mailbox.incinerate = false` turns this off and `incinerate_after = 14.days` shortens it. The design assumes a mailbox extracts what it needs into domain models, so a receipt's PDF must be attached to the `Expense` itself, not left inside the raw email.
go deeper
Know that processed inbound emails are deleted after 30 days and that important data must be saved elsewhere.
Explain which statuses count as processed, how IncinerationJob is scheduled and which settings change the delay or disable it.
Extract attachments and fields into domain models, monitor failed emails before they expire, and check that the queue holds month-long jobs.
Own the retention policy for inbound personal data, aligning incinerate_after with legal requirements and what the business truly needs.
## What incineration is Action Mailbox keeps every inbound message as an `InboundEmail` row plus the full raw source in Active Storage. **Incineration** is the built-in retention rule that deletes both once the message has been handled and a grace period has passed. The point is data minimisation: an inbound email can contain anything a stranger typed, and the app should not keep it longer than it needs for debugging and forensics. ## The mechanism, step by step 1. The mailbox finishes and the status changes to **`delivered`**, **`failed`** or **`bounced`**, which `processed?` treats as processed. A `pending` or `processing` email is never incinerated. 2. The `Incineratable` concern has `after_update_commit :incinerate_later`, guarded by `ActionMailbox.incinerate`, `status_previously_changed?` and `processed?`. 3. `incinerate_later` calls `ActionMailbox::IncinerationJob.schedule`, which is `set(wait: ActionMailbox.incinerate_after).perform_later(inbound_email)`. 4. When the job runs, `InboundEmail#incinerate` builds an `Incineration` that calls `destroy!` only if the email is **due** and still **processed**. 5. **Due** means `updated_at < incinerate_after.ago.end_of_day`, so a status change after scheduling, such as a reroute, keeps the record alive. 6. `destroy!` removes the row, and the `has_one_attached :raw_email` association purges the stored message through Active Storage's own purge job. `IncinerationJob` is declared with `discard_on ActiveRecord::RecordNotFound`, so an email deleted in the meantime does not fail the job. ## Settings | Setting | Default | Effect | |---|---|---| | `config.action_mailbox.incinerate` | `true` | `false` stops scheduling incineration | | `config.action_mailbox.incinerate_after` | `30.days` | delay between processing and deletion | | `config.action_mailbox.queues.incineration` | `nil` on 6.1+ defaults | queue for `IncinerationJob`, so `"default"` | Because each job is scheduled a month ahead, the queue backend must hold scheduled jobs that long. A database-backed queue keeps them as rows until they are due; if scheduled jobs are lost, processed emails are simply never incinerated. ## What the mailbox must do first The guide's intent is explicit: extract everything you need while processing and persist it in your own models. For a bookkeeping app that turns forwarded receipts into expenses: - **Copy attachments.** Attach the receipt's bytes to `Expense#receipt` with Active Storage; do not keep a reference to the `raw_email` blob, which is purged with the inbound email. - **Copy the fields you show later**: sender, subject, date, amount parsed from the body. - **Keep a pointer if you need traceability.** Storing `inbound_email.message_id` on the expense survives incineration and lets you match support questions to logs. - **Do not use `InboundEmail` as an audit log.** It disappears by design; write your own records if regulations require keeping evidence. ## Failed emails are on the clock too `failed` counts as processed, so a message that crashed your mailbox is deleted after the same period. That makes monitoring urgent: find failed `InboundEmail` rows, fix the bug and reroute them (the development conductor has a reroute button; in production set the record back to `pending` and call `route_later`) before the deadline. Raising `incinerate_after` buys time but keeps every message longer, which works against the reason incineration exists. ## Common misreadings - **"Incineration runs on a schedule."** There is no nightly sweep. Each processed email gets its own delayed `IncinerationJob` at the moment its status changes. - **"Turning it off is harmless."** With `incinerate = false`, raw messages accumulate forever in the database and in Active Storage, along with whatever personal data they contain. - **"The conductor's incinerate button deletes at once."** It calls the same `incinerate` method, which still checks that the email is processed and due, so a fresh email survives the click. - **"Pending mail expires too."** Only processed mail is scheduled; a `pending` email stuck because no worker consumes the routing queue stays forever, which is another reason to monitor it. ## Checking it in production Two quick queries tell you whether retention works: the count of processed `InboundEmail` rows older than `incinerate_after` should stay near zero, and the oldest row's `updated_at` should never be much older than the retention period. A growing tail usually means scheduled `IncinerationJob`s were lost or are stuck in a queue no worker consumes.
- Why might an InboundEmail survive past its scheduled incineration job?The job only destroys the record if it is still processed and its `updated_at` is earlier than the end of the day `incinerate_after` ago. If the email was rerouted or otherwise updated after the job was scheduled, `updated_at` is newer, so the old job does nothing; the later status change schedules a fresh job.
- How would you keep inbound mail for 90 days for a compliance review?Set `config.action_mailbox.incinerate_after = 90.days`, accept that the queue must hold jobs that long, and document the retention period. Better still, copy the specific evidence you need into your own models and keep the shorter default, since raw messages carry far more personal data than the review needs.
- Incineration deletes emails 30 days after they are received, processed or not.
- A failed InboundEmail is kept until someone fixes and reroutes it.
- An Expense can keep pointing at the raw_email blob after incineration.
- Incineration is a nightly recurring task configured in recurring.yml.
- Setting incinerate_after to zero turns incineration off.
Assumes Rails 8.1 with load_defaults 8.1: incinerate_after defaults to 30 days and action_mailbox.queues.incineration is nil (since the 6.1 defaults), so IncinerationJob uses the "default" queue.