Disclaimer


We cannot be held responsible for any damage that may result from downloads, installation, storage, or use of software, scripts, or content from our website.

Please be aware, by default, Gmail never deletes messages from your inbox. It archives them.
As a result, it’s not uncommon to reach the limit of available space, even when you have just deleted all your messages.


Gmail places them, not in an "Archives" section, but in "All mail". This results in a folder containing all the good messages, plus all the deleted messages.
And deal with that!

When you only have a few pages, it might be manageable. But if you're hitting the default 15GB limit, it is because you have hundreds of thousands of messages to sort through.

To remedy this, we will need to automate the deletion of our archived messages. And we are going to use jQuery.

Firstly, we need to understand how to detect an archived message from one that is still in our inbox. It’s quite simple, Gmail puts the folder in which it is classified, just before the subject. So if a message does not have a folder, it is an archived message and hence needs to be deleted.

"> image


While archived emails do not have one.

"> image-1

Here are the steps of our script:

  1. Inject jQuery into the page
  2. Go to the page that contains all messages
  3. Detect the number of messages per page, and the total number of messages to extrapolate pagination
  4. Loop through all pages, to select archived messages and delete them.

To inject JavaScript, we open the console and execute this:

var jquery_injection = document.createElement('script');
jquery_injection.setAttribute('src','https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js');
document.head.appendChild(jquery_injection);

Wait a few seconds for the page to finish loading the library and then proceed to the next step.

console.log('Display all message (including archive)');
window.location.href=GM_SESSION_PATH+'#all';

setTimeout(() => { 
    var msg_total=$('div.G-atb .Dj span.ts').eq(2).html(); 
    var msg_per_page= $('div.G-atb .Dj span.ts').eq(1).html();
    var nb_page=parseInt(msg_total.replace(/\s/g, ''))/parseInt(msg_per_page.replace(/\s/g, ''));
    nb_page=Math.ceil(nb_page);
    console.log('Found '+nb_page+' pages');
    console.log('Start cycling through pages');
    setTimeout(() => { 
        Gmail_go_to_page(nb_page);
    }, 1000);
 
}, 1000);
 
function Gmail_go_to_page(page)
{
    console.log('deleting page : '+page);
 
    if(page >= 0 ){
        window.location.href=GM_SESSION_PATH+'#all/p'+page;
        setTimeout(() => { 
            Gmail_delete_archive(page);}, 5000);
    }
     
}
 
function Gmail_delete_archive(page)
{
    console.log('deleting messages');
    document.head.appendChild(jquery_injection);
    $('.UI table[role=grid]:visible').eq(0).find('tr').each(function(i,e){
        if($(e).find('.at').length==0){
            $(e).find('ul[role=toolbar] li').eq(1).trigger('click');
		console.log('.');
        }
 
    });
    if(page-1 >= 0 ){
        setTimeout(() => { Gmail_go_to_page(page-1);}, 10000);
    }else{
        console.log('Cycling ended !!');
    }
 
}

Once the loop is completed, the message "Cycling ended" is displayed.

"> image-2