IMAP: \Deleted set on a message in one mailbox also marks its copy in another, so EXPUNGE there deletes the wrong copy

Issue Description

Within one account, IMAP COPY does not create a copy. It adds the destination mailbox to the same stored message, and that message keeps one set of flags. So \Deleted stored on the message in mailbox C1 also shows on the “copy” in C2. A client that then runs EXPUNGE with C2 selected removes the C2 copy, which the user never marked for deletion. The C1 copy that the user did mark survives, and its \Deleted flag is cleared.

The same happens when a Sieve script files one delivery into two mailboxes (fileinto :copy "B1"; fileinto "B2";).

Expected Behavior

In IMAP, each mailbox’s messages have their own flags. RFC 9051 §6.4.7: COPY “copies the specified message(s) to the end of the specified destination mailbox”, and a STORE in one mailbox does not change a copy in another. EXPUNGE removes “all messages that have the \Deleted flag set from the currently selected mailbox” (RFC 9051 §6.4.3), so an EXPUNGE in C2 should remove nothing here. Dovecot and Cyrus, for example, store a separate copy, with its own flags, per mailbox.

Actual Behavior

Tested on v1.0.3: the official stalwartlabs/stalwart:v1.0.3 image. Two IMAP sessions, A and B, logged in to the same account (untagged SELECT details trimmed):

A: a4 APPEND C1 {73}
A: a4 OK [APPENDUID 3389567515 1] APPEND completed
A: a5 SELECT C1
A: a5 OK [READ-WRITE] SELECT completed
A: a6 COPY 1 C2
A: a6 OK [COPYUID 439401335 1 1] COPY completed
B: b2 SELECT C2
B: b2 OK [READ-WRITE] SELECT completed
A: a7 STORE 1 +FLAGS (\Deleted)
A: * 1 FETCH (FLAGS ($hasnoattachment \Deleted))
B: b3 FETCH 1 FLAGS
B: * 1 FETCH (FLAGS ($hasnoattachment \Deleted))      <- C2's copy is marked too
B: b4 EXPUNGE
B: * 1 EXPUNGE                                          <- C2's copy is removed
A: a8 NOOP
A: * 1 FETCH (FLAGS ($hasnoattachment) UID 1)           <- C1's copy is kept, \Deleted cleared
B: b5 STATUS C2 (MESSAGES)
B: * STATUS "C2" (MESSAGES 0)

So the copy the user kept is gone, and the copy they deleted is kept and no longer marked deleted. This is mail loss for ordinary clients. A “copy to folder, then delete the original” in any
client that marks messages deleted and expunges later (mutt, Thunderbird’s “mark as deleted” mode, a client that expunges on folder close) loses the copy, if the destination folder is expun
ged before the source. It also breaks programs that process a mailbox over IMAP while another program works on a different mailbox holding the same message: each sees the other’s \Deleted and retry keywords.

Reproduction Steps

  1. Log in twice to one account, as sessions A and B.
  2. A: APPEND any message to C1, SELECT C1, COPY 1 C2.
  3. B: SELECT C2.
  4. A: STORE 1 +FLAGS (\Deleted).
  5. B: FETCH 1 FLAGS shows \Deleted. Then EXPUNGE: C2 is now empty.
  6. A: C1 still holds the message, without \Deleted.

The same result follows if, instead of step 2, a Sieve script files one delivery into C1 and C2 (fileinto :copy "C1"; fileinto "C2";).

Stalwart Version

v1.x

Installation Method

Docker

Database Backend

RocksDB

Blob Storage

RocksDB

Search Engine

Internal

Directory Backend

Internal

Additional Context

Cause
Flags (keywords) belong to the message document, not to its membership in a mailbox, and a same-account COPY adds a mailbox to the existing document:

  • crates/imap/src/op/copy_move.rs, v1.0.3, line 355: same-account COPY is new_data.add_mailbox(dest_mailbox_id) on the source message’s data. No new message is created.
  • crates/imap/src/op/expunge.rs, line 177: the messages to expunge are cache.in_mailbox_with_keyword(mailbox_id, &Keyword::Deleted), i.e. any message in the selected mailbox whose single, shared keyword set contains \Deleted.
  • expunge.rs, lines 318-323: when the message is in more than one mailbox, expunge removes the selected mailbox and then remove_keyword(&Keyword::Deleted). That is why the copy in C1 loses its \Deleted mark.

Sieve delivery to several mailboxes makes the same single document (crates/email/src/sieve/ingest.rs, line 829: one email_ingest with mailbox_ids: sieve_message.file_into).

Suggested Fix
Make \Deleted per mailbox. For example, store the mailboxes in which the message is marked deleted with each mailbox membership (next to its UID), use that in STORE, FETCH, SEARCH and EXPUNGE, and do not show it on other mailboxes. Alternatively, have same-account COPY create a new message that shares the blob, as a cross-account copy already does (line 540 onwards).

I have reviewed the documentation and FAQ and confirm that my issue is NOT addressed there.

on

I have searched this support forum (open and closed topics) and confirm this is not a duplicate.

on

I understand that topics in this category are triaged by a bot first but a human reply will follow up. If I’d prefer a human-only reply, I’ll add the no-ai tag to my topic.

on

The code you cite is in the current source, but I didn’t find any documentation that covers this behaviour.

The relevant lines match your description:

  • crates/imap/src/op/copy_move.rs, line 355: a same-account COPY calls new_data.add_mailbox(dest_mailbox_id) on the existing message data.
  • crates/imap/src/op/expunge.rs, lines 322-323: when a message is in more than one mailbox, EXPUNGE removes the selected mailbox. It then calls remove_keyword(&Keyword::Deleted) on the message.

So the keyword set is shared across all of a message’s mailboxes. That explains both the shared \Deleted flag and the cleared flag on the surviving C1 copy.

I haven’t confirmed whether this is intended or planned to change. A staff member will need to say whether it’s a bug and whether per-mailbox \Deleted is planned.

This isn’t covered in the current docs. A community member or staff member will follow up.

This is an automated reply from the Stalwart Help Bot. Other community members may follow up if this answer is incomplete or wrong.

This is how Stalwart is designed. It’s a JMAP-first server, and in JMAP one Email can belong to several mailboxes at once, with a single set of keywords. A COPY within the same account follows that model: it adds the destination mailbox to the existing message as a second reference, so both “copies” are the same message and share every flag, \Deleted included. The code you traced in copy_move.rs and expunge.rs is doing what it’s meant to do. Cross-account copies are different, because the message has to be stored again in the other account.

For the copy-then-delete pattern, IMAP MOVE (RFC 6851) avoids the problem entirely, since the message just changes mailbox. Clients that expunge the source mailbox first are fine as well: the expunge drops that mailbox from the message and clears \Deleted, and the copy in the destination stays.

Thanks. I understand the JMAP model for keywords, but \Deleted isn’t a JMAP keyword. RFC 8621 §4.1.1 leaves it out because JMAP has no delete+expunge model, and says any message with it “MUST NOT be visible via JMAP”. So keeping \Deleted per mailbox wouldn’t change the JMAP model: JMAP keywords would still be one set per Email. Stalwart already keeps per-mailbox state for each Email (the IMAP UID).

The shared flag also goes against RFC 8621 itself. Marking the INBOX copy deleted hides the Archive copy from JMAP clients too, while §2 says an Email in two mailboxes SHOULD be treated as existing in both places.

It can also lose mail outright. Sieve fileinto :copy “Archive”; keep; gives one Email in INBOX and Archive:

1. A client marks the INBOX copy \Deleted, which also marks the Archive copy.

2. Another client expunges Archive. The Archive reference is removed, and the message reappears undeleted in INBOX.

3. The user deletes it from INBOX again, and it’s gone from both. The copy they kept as a backup no longer exists.

This is the exact use case I have. I filter all incoming email into both Inbox and Archive, so that I can freely delete mail from my Inbox without worrying if I might need to refer back to it later.

Would you consider keeping \Deleted per mailbox?

Hi,

My earlier reply was too broad on one point. \Deleted isn’t a JMAP keyword, and because of the RFC 8621 visibility rule a message marked deleted in INBOX also disappears from Archive for JMAP clients. Even so, we’re keeping the single-message model: one Email with one keyword set, whichever mailboxes it’s in, and \Deleted stays part of that set. Making it per mailbox would change how message data is stored for the sake of one flag, and that’s not something we plan to do.

For your Inbox plus Archive setup, mail is only lost when a \Deleted mark set in one folder is still pending while another folder gets expunged. If your clients delete by moving to Trash, or expunge as soon as you delete (in Thunderbird, the “Move it to the Trash” or “Remove it immediately” options, not “Just mark it as deleted”), deleting from Inbox only removes the Inbox membership and the Archive copy stays put. Emptying Trash later works the same way, since the message is still in Archive.