Delete email permanently in GMail

If you delete a message from your Inbox or one of your custom folders, it will still appear in [Gmail]/All Mail.

Here’s why: in most folders, deleting a message simply removes that folder’s label from the message, including the label identifying the message as being in your Inbox.

[Gmail]/All Mail shows all of your messages, whether or not they have labels attached to them.

If you want to permanently delete a message from all folders:

  1. Move it to the [Gmail]/Trash folder.
  2. Delete it from the [Gmail]/Trash folder.

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.

Here’s how this looks like using Mail.dll IMAP client:

// C# version:

using(Imap imap = new Imap())
{
	imap.Connect("imap.gmail.com");
	imap.Login("user", "password");

	// Find all emails we want to delete
	imap.SelectInbox();
	List<long> uids = imap.Search(
		Expression.Subject("email to delete"));

	// Delete each email permanently
	foreach (long uid in uids)
	{
		// Move to Trash
		long uidInTrash = (long)imap.MoveByUID(uid, "[Gmail]/Trash");

		// Delete from Trash
		imap.Select("[Gmail]/Trash");
		imap.DeleteMessageByUID(uidInTrash);
	}
	imap.Close(true);
}
' VB.NET version:

Using imap As New Imap()
	imap.Connect("imap.gmail.com")
	imap.Login("user@gmail.com", "password")

	' Find all emails we want to delete
	imap.SelectInbox()
	Dim uids As List(Of Long) = imap.Search(_
		Expression.Subject("email to delete"))

	' Delete each email permanently
	For Each uid As Long In uids
		' Move to Trash
		Dim uidInTrash As Long = imap.MoveByUID(uid, "[Gmail]/Trash")

		' Delete from Trash
		imap.[Select]("[Gmail]/Trash")
		imap.DeleteMessageByUID(uidInTrash)
	Next
	imap.Close(True)
End Using

Tags: , ,

4 Responses to “Delete email permanently in GMail”

  1. Pintu Says:

    Topic: deleting email permanently

    long uidInTrash = (long)imap.MoveByUID(uid, “[Gmail]/Trash”);

    from this line of code get the gollwing error:
    Error 1 Cannot convert type ‘void’ to ‘long’

  2. Pawel Lesnikowski Says:

    @Pintu Make sure you have the latest version installed.

  3. Farooq Says:

    How to do the same for POP3 accounts?

  4. Pawel Lesnikowski Says:

    @Farooq
    POP3 protocol does not have a concept of seen/unseen emails nor folders.

    If you need to delete an email just use Pop3.DeleteMessage or Pop3.DeleteMessageByUID method.

Leave a Reply