Skip to content

Instantly share code, notes, and snippets.

@olegwtf
Created February 25, 2019 10:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olegwtf/228d32a31a1b704a3e42638492d2cdf6 to your computer and use it in GitHub Desktop.
Save olegwtf/228d32a31a1b704a3e42638492d2cdf6 to your computer and use it in GitHub Desktop.
Script to remove messages by label from gmail via IMAP
use strict;
use warnings;
use Net::IMAP::Simple;
use constant {
LOGIN => 'xxx@gmail.com',
PASSWORD => '123',
MAILBOX => 'Label', # folder to delete from, some label may be used here
TRASH => '[Gmail]/&BBoEPgRABDcEOAQ9BDA-', # trash folder name for ru users, should be '[Gmail]/Trash' for en
};
my $imap = Net::IMAP::Simple->new("imap.gmail.com:993", use_ssl => 1)
or die "connection failed: ", $Net::IMAP::Simple::errstr;
$imap->login(LOGIN, PASSWORD)
or die "login failed: ", $imap->errstr;
my $cnt = $imap->select(MAILBOX)
or die "select failed: ", $imap->errstr;
# to completely delete message from gmail your first need to move it to the trash
$imap->copy("1:$cnt", TRASH)
or die "copy: ", $imap->errstr;
$imap->delete("1:$cnt")
or die "delete failed: ", $imap->errstr;
$imap->expunge_mailbox(MAILBOX)
or die "expunge failed:", $imap->errstr;
# then clear trash
$cnt = $imap->select(TRASH)
or die "select failed: ", $imap->errstr;
$imap->delete("1:$cnt")
or die "delete failed: ", $imap->errstr;
$imap->expunge_mailbox(TRASH)
or die "expunge failed:", $imap->errstr;
$imap->quit();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment