Overview
Permanently deletes a message from the specified mailbox. The message is marked with the\Deleted flag and immediately expunged from the mailbox. This operation is irreversible and the message cannot be recovered.
This operation is destructive and permanent. The message will be permanently removed from the mailbox and cannot be recovered. Consider moving the message to a Trash folder using
imap_move_message instead if you want the ability to recover deleted messages.Confirmation requirement
Ifconfirm is not set to true, the request will be rejected with an invalid_input error.
Common use cases
- Permanently remove spam or unwanted messages
- Delete messages after processing or archiving elsewhere
- Clean up test or temporary messages
- Implement retention policies for old messages
- Free up mailbox storage space
Input parameters
Account identifier. Must match pattern
^[A-Za-z0-9_-]{1,64}$.Message identifier in format
imap:{account_id}:{mailbox}:{uidvalidity}:{uid}.The account_id in the message_id must match the account_id parameter.Must be set to the literal boolean value
true to confirm the deletion.This is a safety mechanism to prevent accidental deletions.Response
Human-readable one-line outcome:
"Message deleted"Example request
Example response
Error responses
Implementation notes
Deletion process
The deletion operation consists of three steps:-
Connect and authenticate (
steps_attempted=1)- Opens a connection to the IMAP server
- Authenticates with the account credentials
- Selects the mailbox in read-write mode
- Verifies the message’s uidvalidity matches the current mailbox state
-
Mark as deleted (
steps_attempted=2)- Adds the
\Deletedflag to the message usingUID STORE +FLAGS.SILENT (\Deleted) - The message remains visible but is marked for deletion
- Adds the
-
Expunge (
steps_attempted=3)- Permanently removes the message using
UID EXPUNGE - The message is immediately removed from the mailbox
- The message cannot be recovered after this step
- Permanently removes the message using
Partial deletion states
Ifsteps_succeeded=2 but the expunge fails:
- The message has the
\Deletedflag set - The message is still visible in the mailbox (to IMAP clients that show deleted messages)
- The next EXPUNGE operation (from any client) will permanently remove it
- Some email clients hide messages with
\Deletedflag automatically
Safety guarantees
- Explicit confirmation required: The
confirmparameter must be exactlytrue - Write gate check:
MAIL_IMAP_WRITE_ENABLED=truemust be set - UIDValidity verification: Ensures the message hasn’t changed since the message_id was obtained
- Atomic flag+expunge: Uses
UID EXPUNGEto expunge only the specific message, not all deleted messages
Recovery options
Once a message is expunged, it cannot be recovered through the IMAP protocol. Some email providers may offer server-side recovery options through their web interfaces, but this is not guaranteed.
- Use
imap_move_messageto move to a Trash folder - Implement a soft-delete workflow with custom flags
- Archive messages to external storage before deletion
See also
- imap_move_message - Move message to Trash (recoverable)
- imap_update_message_flags - Add
\Deletedflag without expunging - Write operations configuration - Enable write operations
- Message IDs - Understanding message identifier format