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.ConnectSSL("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"));

	// Move email to Trash
	List<long> uidsInTrash = new List<long>();
	foreach (long uid in uids)
	{
		long uidInTrash = (long)imap.MoveByUID(uid, "[Gmail]/Trash");
		uidsInTrash.Add(uidInTrash);
	}

	// Delete moved emails from Trash
	imap.Select("[Gmail]/Trash");
	foreach (long uid in uidsInTrash)
	{
		imap.DeleteMessageByUID(uid);
	}

	imap.Close();
}
' VB.NET version:

Using imap As New Imap()
	imap.ConnectSSL("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"))

	' Move email to Trash
	Dim uidsInTrash As New List(Of Long)
	For Each uid As Long In uids
		Dim uidInTrash As Long = imap.MoveByUID(uid, "[Gmail]/Trash")
		uidsInTrash.Add(uidInTrash)
	Next

	' Delete moved emails from Trash
	imap.[Select]("[Gmail]/Trash")
	For Each uid As Long In uidsInTrash
		imap.DeleteMessageByUID(uid )
	Next

	imap.Close()
End Using

You can also use bulk methods:

// C# version:

using(Imap imap = new Imap())
{
	imap.ConnectSSL("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"));

	// Move emails to Trash
	List<long> uidsInTrash = imap.MoveByUID(uids, "[Gmail]/Trash");

	// Delete moved emails from Trash
	imap.Select("[Gmail]/Trash");
	imap.DeleteMessageByUID(uidsInTrash);

	imap.Close();
}
' VB.NET version:

Using imap As New Imap()
	imap.ConnectSSL("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"))

	' Move email to Trash
	Dim uidsInTrash As List(Of Long) = imap.MoveByUID(uids, "[Gmail]/Trash")

	' Delete moved emails from Trash
	imap.[Select]("[Gmail]/Trash")
	imap.DeleteMessageByUID(uidsInTrash )

	imap.Close()
End Using

Tags: , ,

12 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.

  5. vinay Bammidi Says:

    Hi,

    I like to delete my sent mails in gamil. how can i do this.

  6. Pawel Lesnikowski Says:

    @Vinay
    Instead of
    imap.SelectInbox()
    use
    imap.Select("[Gmail]/Sent Mail")

    If you want to delete all emails in this folder replace the search code with:
    List<long> uids = imap.GetAll();
    -or-
    Dim uids As List(Of Long) = imap.GetAll()

  7. vinay Bammidi Says:

    A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 74.125.155.109:143

    I am getting this error.Can you please tell me how to fix this.

  8. Pawel Lesnikowski Says:

    @Vinay

    Most likely your server does not require SSL. Just use Imap.Connect method in that case. Please also read this post: Connection attempt failed.

    Please contat us directly if you have other connection problems.

  9. hiremath V K Says:

    hi,
    i want to delete mails from outlook.
    plz Ans,.
    thank you.

  10. Pawel Lesnikowski Says:

    @hiremath

    With Mail.dll you can delete and manage email on any IMAP or POP3 server (including Exchange).

    Outlook is a client application. If you want to delete email from a local pst store, Mail.dll is not going to help you, but if you want to delete emails from Exchange read this post: Delete email messages using IMAP. You may need to establish SSL connection but it’s quite easy just use ConnectSSL method.

  11. fireandhemlock Says:

    hey, I was wondering, whether similar code can be used to immediately delete mails from ONE particular contact?

  12. Pawel Lesnikowski Says:

    @fireandhemlock

    You just need to change the search code.

    The following code searches for all email sent from alice:

    List<long> uids = imap.Search(Expression.From("alice@company.org"));
    
    Dim uids As List(Of Long) = imap.Search(Expression.From("alice@company.org"))
    

    Read more about searching emails on IMAP server.

    If you are using Gmail you also may also use Gmail’s specific search syntax (same as on Gmail web site).

Leave a Reply