Steps:
1. Open Google Apps Script (drive.google.com > new > more > Google Apps Scripts)
2. Click create scripts.
3. Write the Script:
function deleteOldEmails() {
var threads = GmailApp.search('older_than:1y');
for (var i = 0; i < threads.length; i++) {
threads[i].moveToTrash();
}
}
This example deletes emails older than one year (older_than:1y). You can modify the search query for different criteria (e.g., label:unread, from:[email protected], before:YYYY/MM/DD).4. Save the Script.
5. Run the Script:
Click the play icon to run the script. It will ask for permissions the first time.
This method allows you to automate email management using custom queries.
If you want to test the search string - GmailApp.search(...) - to be sure you are deleting the right emails, you can use the filter on the search bar on Gmail.