Step 1: Login and Create a Project on Google Apps Script
Go to https://script.google.com/ and sign in with your Google account. Then, create a new project.
Step 2: Run the Script
Copy and paste the following script into your Google Apps Script editor:
function deleteOldEmails() {
const threads = GmailApp.search('older_than:1d');
for (let i = 0; i < threads.length; i++) {
threads[i].moveToTrash();
}
}
deleteOldEmails();
This script will delete emails older than 1 day. Adjust the older_than
parameter to fit your needs.
Step 3: Rerun the Script Using the Console
Due to script execution time limits (5 minutes), you may need to rerun the script manually. You can do this using the browser console:
// Select the element to monitor for changes in the disabled property
const elementToMonitor = document.querySelector('button[aria-label="Запустити вибрану функцію"]');
// Create a MutationObserver instance
const observer = new MutationObserver((mutationsList, observer) => {
for(const mutation of mutationsList) {
// Check if the mutation is related to the 'disabled' property
if (mutation.attributeName === 'disabled') {
if (!mutation.target.hasAttribute('disabled')) {
console.log('Triggered click');
// Trigger a click event when disabled property is removed
mutation.target.click();
}
}
}
});
// Start observing the element for attribute changes
observer.observe(elementToMonitor, { attributes: true });
This script will automatically click the button to rerun the email deletion process when the disabled property is removed.
This guide provides a straightforward method to automate the cleanup of old emails in Gmail using Google Apps Script and JavaScript. Enjoy a clutter-free inbox!