If you want to delete a message from all Gmail folders using IMAP:
- Move it to the [Gmail]/Trash folder.
- Delete it from the [Gmail]/Trash folder.
Use CommonFolders class to get actual folder names (it may be different in localized Gmail's versions)
All emails in [Gmail]/Spam and [Gmail]/Trash are deleted after 30 days. If you delete a message from [Gmail]/Spam or [Gmail]/Trash, it will be deleted permanently.
This is the code:
using(Imap imap = new Imap())
// Recognize Trash folder
List<FolderInfo> folders = imap.GetFolders();
CommonFolders common = new CommonFolders(folders);
FolderInfo trash = common.Trash;
// Find all emails we want to delete
List<long> uids = imap.Search(Expression.Subject("email to delete"));
// Move email to Trash
List<long> uidsInTrash = new List<long>();
foreach (long uid in uids)
long uidInTrash = (long)imap.MoveByUID(uid, trash);
// Delete moved emails from Trash
foreach (long uid in uidsInTrash)