Home › Guides › Attachments

What Happens to Attachments After a Message Expires?

An attachment does not get an independent extension beyond its email. In TempMailPortal, an attachment is reachable only through the message that received it. When that message reaches its selected expiry time, the message-detail route and the attachment-download route stop returning it. A scheduled cleanup later removes eligible attachment objects and their database references.

That difference matters. “No longer accessible” is an immediate API rule. “Physically removed from storage” is a cleanup operation that can happen later. TempMailPortal should not promise that every stored byte is erased at the exact second a ten-minute, one-hour or 24-hour timer ends.

Metadata and file bytes are not the same thing

When the receiving Worker parses an email, it builds attachment metadata containing a filename, MIME type and byte size. For an eligible file, it also stores the bytes in a private Cloudflare R2 bucket and records an internal object key in the message's attachment list. The key is not presented as a public URL.

Current ingestion limits allow no more than 20 attachments on one incoming message. An individual attachment is copied to R2 only when it has content and is no larger than 15 MiB. A larger file can still leave metadata in the message record without a stored object key, so the presence of a filename does not guarantee that the bytes are downloadable.

The R2 bucket binding is private. The Worker reads an object only after it has authenticated the mailbox, checked the app/account boundary, found the unexpired message and located the requested attachment index. This design avoids permanent public file links, but it is not a malware scanner and it does not make an unknown file safe.

The website and Android app intentionally differ

The browser inbox displays attachment names and sizes. Android can download a retained attachment when it supplies the mailbox credential, a valid Firebase identity and valid Android App Check proof. Automated tests confirm that a copied website credential receives a forbidden response before the R2 object is read, while a verified app/account request can download it.

For the reader, the practical rule is simpler: seeing a filename on the website is not an invitation to open it there. Use the Android app only for a file you expected from a sender you recognize, and keep a separate trusted copy if you are permitted to retain it. Do not use a disposable inbox for documents that must remain available.

Expiry blocks all message-scoped access

The Worker stores an expires_at timestamp on the message. Three relevant database queries require that timestamp to be in the future:

  1. the inbox list;
  2. the full message detail, which contains attachment metadata; and
  3. the attachment route, which retrieves the private R2 object.

After the timestamp passes, those routes behave as if the message is not found. This check happens before an attachment object is fetched. Even if a cleanup job has not yet removed the R2 object, the normal product route should no longer expose it.

The message lifetime is calculated from when TempMailPortal receives the email, not when the reader opens it. It is also capped by the mailbox's own expiry. A file received near the end of a mailbox's life may therefore have less time available than the selected message-retention option suggests.

Physical cleanup runs later

TempMailPortal configures a periodic Cloudflare Cron Trigger. Its scheduled handler selects the oldest expired messages in batches of up to 200 per run. For each batch, it collects retained attachment keys, deletes those keys from R2, and only then deletes the matching message rows from D1.

Cloudflare's Cron Trigger documentation explains that scheduled handlers run on configured UTC schedules. A periodic job is not a per-message erasure timer. If a message expires just after a run, or if more expired messages are waiting than one batch can process, physical cleanup can lag behind access expiry.

The Worker groups R2 deletions in batches of at most 1,000 keys. Cloudflare's R2 Workers API reference documents that limit and says a successful delete is strongly consistent: subsequent reads no longer see those keys after the promise resolves.

Why attachment objects are deleted before message rows

Deleting the D1 message row first would throw away the list of R2 keys that still need cleanup. The current order avoids that. If an R2 deletion fails, the Worker logs the failure and throws before deleting the message rows. A later scheduled run can use the retained references to try again.

Automated tests exercise this behavior: a simulated R2 failure leaves the D1 row in place, then a later successful run removes it. Another test seeds 250 expired messages; the first run removes 200 and the next removes the final 50. These are local implementation tests, not a claim that a live production sweep has been observed on the same date.

An explicit inbox or account deletion follows the same broad principle. The service gathers attachment keys and deletes the private objects before removing related database records. If the message could not be inserted after an attachment was uploaded during receipt, the ingestion path also attempts to delete the newly created object keys. These safeguards reduce orphaned files, but no software path justifies promising that an object can never become orphaned during an infrastructure failure.

A bucket lifecycle rule can be a backstop, not the access control

Cloudflare supports bucket-level R2 object lifecycle rules that can expire objects by age or prefix. Cloudflare notes that objects are typically removed within 24 hours of the lifecycle expiration value and that large migrations can take longer. That makes lifecycle deletion suitable as a defense-in-depth backstop, not as the reader-facing expiry boundary.

On 3 December, we verified that the private production bucket had an enabled one-day object lifecycle rule. The rule serves as a backstop for leftover objects. The Worker uses the message expires_at check to control when file access ends.

Download responses reduce caching, not file risk

For an authorized download, TempMailPortal sends Cache-Control: no-store and X-Content-Type-Options: nosniff, and uses an attachment content disposition with a sanitized filename. Those headers tell compatible clients not to store the response in ordinary caches and not to reinterpret the declared content type. They do not scan the file, prove who created it or prevent every device-level copy.

Treat any unexpected attachment as untrusted. Do not open executables, archives, macro-enabled documents or files whose content does not match the sender's explanation. Do not use a temporary inbox for healthcare, employment, school, government, financial or account-recovery documents. If a legitimate workflow needs a durable file, move it through an approved storage process before the message expires.

What a user should expect

Before expiry, a verified Android app/account request may download a retained attachment within the applicable quotas. The website can show only the metadata. At expiry, the message detail and download route stop returning the attachment. Afterward, periodic bounded cleanup removes retained R2 objects and the message row; a failed object deletion is designed to leave references available for retry.

That is the safest way to describe the lifecycle: access ends at the message boundary, storage reclamation follows, and neither the file's name nor its presence in private storage makes it safe or permanent.

Sources and verification

Attachment metadata, authorization and expiry handling were checked against the current Worker implementation. We verified the lifecycle setting and observed one synthetic attachment in private production storage before its expiry. File removal after that deadline and a production Android download were outside those checks.