Skip to main content

ClearBody

procedure ClearBody;

Example

procedure RemoveMessageContent(const Message: TIdMessage);
begin
Message.ClearBody;
end;

Usage

ClearBody destroys every owned MIME message part and then clears the separate preamble/body string list without changing header properties.

Additional Technical Info

ClearBody first calls MessageParts.Clear, then calls Body.Clear. MessageParts owns its items, so all text parts and attachments are destroyed. Any borrowed part/body assumptions must be discarded; received temporary attachment cleanup can also occur during destruction.

The Body list is the message-level plain body or MIME preamble, not the collection of TIdText bodies. Clearing one without the other would not remove all message content, which is why this method clears both.

Header and configuration state is not changed. ContentType, ContentTransferEncoding, Encoding, raw Headers, recipients and MIMEBoundary can therefore describe content that no longer exists until the caller resets or regenerates them. Use Clear when both header and body state must be reset.

The message-parts cached AttachmentCount/TextPartCount fields are not maintained by TCollection.Clear; they can remain stale even though Count=0. Call MessageParts.CountParts before reading those properties, or rely on GenerateHeader, which recounts first.

The two clears are non-transactional: a destruction exception can prevent Body from being cleared. Cost is proportional to part cleanup plus body size, and concurrent access is unsupported. The source-reviewed example was not runtime-tested.

External references

Created 2026-07-15