Message logging and retention for WhatsApp products
What to log, how long to keep it and how to purge it: a log design for WhatsApp traffic that supports support, billing and audits without hoarding content.
Every WhatsApp product ends up with a message table, and every message table ends up being asked three different questions: what happened to this message, what did this customer pay for, and can this person's data be deleted. A log designed only for debugging answers the first and creates problems for the other two. This article separates the kinds of records, suggests retention classes and shows how to keep delivery history without keeping content forever.
What does a WhatsApp message log need to contain?
Split it into metadata and content. Metadata (ids, direction, timestamps, status, error code, template name, pricing category) is cheap, low-risk and valuable for years of debugging and billing. Content (message text, media, contact names) is sensitive and should have a shorter, explicit life.
| Record | Examples | Sensitivity | Typical use |
|---|---|---|---|
| Event metadata | Message id, channel, direction, status, error code, timestamps | Low to moderate (phone numbers are personal data) | Debugging, delivery reports |
| Pricing data | Billable or free flag and category from status webhooks | Low | Cost attribution and reconciliation |
| Message content | Text, captions, location, contacts | High | Support threads, product features |
| Media | Images, audio, documents | High | Support, evidence; large and costly to store |
| Raw webhook bodies | Payload as received | High (contains content) | Short-term replay and incident analysis |
Meta's status webhooks carry a pricing object: billable messages show a type of regular and free service messages show free_customer_service. Keeping that field next to the message id is what later lets a finance team explain a bill; the cost-attribution article builds on it.
How should the tables be shaped?
Keep metadata and content in separate tables joined by the message id, so content can be purged or tiered without touching the delivery history. A single wide table makes every deletion a trade-off.
CREATE TABLE message_event (
tenant_id uuid NOT NULL,
message_id text NOT NULL, -- provider/wamid
channel_id text NOT NULL,
direction text NOT NULL, -- inbound | outbound
status text NOT NULL, -- accepted | sent | delivered | read | failed
error_code integer,
template text,
pricing_type text, -- regular | free_customer_service | ...
occurred_at timestamptz NOT NULL,
PRIMARY KEY (tenant_id, message_id, status)
);
CREATE TABLE message_content (
tenant_id uuid NOT NULL,
message_id text NOT NULL,
body text,
media_ref text,
expires_at timestamptz NOT NULL, -- set at write time from the tenant's policy
PRIMARY KEY (tenant_id, message_id)
);
CREATE INDEX message_content_expiry ON message_content (expires_at);The primary key on message_event includes status because a single message produces several status updates, and deduplicating on the message id alone would discard them. Setting expires_at at write time, from the tenant's own policy, means retention is data, not a cron job someone must remember.
How long should each kind of record be kept?
There is no single correct period; the right one is the shortest that serves the purpose, set per record class and per tenant. Retention is a product decision made with the customer and, where relevant, their legal adviser, and it should be written down.
A workable pattern is three classes: short for raw webhook bodies (enough to cover retries and incident analysis), medium for message content (long enough to serve the customer's support process) and longer for metadata and pricing (needed for reconciliation and audits). The actual durations depend on the customer's obligations and your own contracts; this article deliberately gives none, because inventing a universal number would be false precision.
Anchor the shortest class to the retry window. Meta retries failed webhook deliveries for up to 7 days, and CRPRO Hub retries for roughly four days, so raw bodies and idempotency records should live at least as long as any replay you might perform; the webhooks reference explains replay.
Should you rely on the provider's history instead of storing your own?
Treat the provider's history as a recovery source, not as your system of record. It is useful for reconciling gaps, but its retention, filters and pagination are the provider's choices, not yours.
The Hub exposes channel message history through a cursor-paginated endpoint, filterable by direction and raw status, and an audit log filterable by action, target type, API key and time range. Both need the logs:read scope. That is enough to rebuild missing inbound messages after a long outage, which the webhook retry layers cannot always cover, but it is not a substitute for storing what your own product needs. Check the provider's documentation for how long history remains available before depending on it.
# recover inbound messages after an outage
curl "https://crprohub.com/api/v1/channels/CHANNEL_UUID/messages?direction=inbound&limit=100" \
-H "Authorization: Bearer $HUB_API_KEY"
# follow next_cursor until it is null, upsert by message idHow do you purge reliably?
Purge with a scheduled job that deletes by expires_at in small batches, logs counts rather than content and runs against replicas and backups through their own expiry. A purge that only touches the primary database leaves the data alive in places nobody checks.
- Delete in batches with a limit to avoid long locks and replication lag.
- Record how many rows were purged per tenant and per class, with timestamps, as evidence the policy runs.
- Include search indexes, caches, object storage for media and analytics copies in the purge.
- Set a backup lifecycle that matches the longest class, and document that deleted rows persist in backups until they roll off.
- Support an on-demand erasure path for a single contact across metadata and content, covered in the privacy article of this series.
How should media be handled in the log?
Store media by reference with its own, usually shorter, expiry, and download it only when a feature needs it. Media is the largest and most sensitive part of a conversation, so it deserves a separate decision rather than inheriting the message retention.
Inbound media arrives as an identifier that the platform lets you resolve and download; copying every file at arrival multiplies storage and risk for files nobody opens. A lazy approach works well: record the media reference in the content table, fetch on demand, cache briefly and delete the cache on schedule. For outbound media, keep the source file only as long as the campaign or conversation needs it. Remember that upload size limits differ by type and are set by the platform, so validate before uploading to avoid failed sends being logged as mystery errors.
- Keep a media_ref and a content type in the log; keep the bytes in object storage with a lifecycle rule.
- Delete cached copies when the owning message content expires.
- Never place media URLs that embed credentials in logs.
- Log failed uploads with the error code so size and type problems are visible.
Who should be able to read the logs?
Reading content should be a narrower permission than reading metadata, and every read of content by a human should be recorded. Most support questions can be answered from metadata alone.
Give support a view that shows status and error codes without message bodies by default, and require a reason to open the content. Mask phone numbers in logs sent to third-party observability tools. Never include credentials in log lines; headers such as Authorization belong to the redaction list, and the authentication page describes what the credentials are so you can recognise them in logs.
Frequently asked questions
Should I store raw webhook payloads?
Briefly, if at all. They help with replay and incident analysis during the retry window, but they contain message content, so give them the shortest retention class.
How long should message content be kept?
Only as long as it serves a defined purpose, such as the customer's support process. Set the period per tenant, write it down and enforce it with an expiry column rather than manual cleanup.
Can I rebuild lost messages from the provider?
Partly. A cursor-paginated history endpoint lets you recover inbound messages after an outage, but confirm how long the provider keeps history before relying on it.
Fontes
Documentação oficial da Meta, conferida em 31 de outubro de 2026.