This project provides a Google Apps Script to automate the management of Gmail emails. The script is designed to:
- Delete all emails from the Promotions and Social categories.
- Delete emails older than one week from the Primary category.
- Exclude starred emails and user-defined emails or labels from deletion.
The script runs daily at midnight to keep your inbox clean and organized.
- All emails from the Promotions and Social categories.
- Emails older than one week from the Primary category.
- Starred emails.
- User-defined email addresses and labels.
- Go to Google Apps Script.
- Click on
New Project
to create a new script or open an existing one.
- Delete any default code in the script editor.
- Copy and paste the provided script code into the editor.
- Replace
'[email protected]'
with your actual email address in theMailApp.sendEmail
function to receive error notifications. - Modify the
excludeEmails
andexcludeLabels
arrays with the email addresses and labels you want to exclude from deletion. - Save the project with a meaningful name, such as “AutoDeleteEmails”.
- Click on the play button (triangle icon) in the toolbar to run the script manually.
- A dialog box will appear asking for authorization. Follow the prompts to grant the necessary permissions to access your Gmail account.
- Click on the clock icon (Triggers) in the toolbar, or go to
Edit
>Current project's triggers
. - Click on
+ Add Trigger
at the bottom right of the Triggers page. - Set the following options in the trigger setup form:
- Choose which function to run:
deleteOldEmails
- Choose which deployment should run:
Head
- Select event source:
Time-driven
- Select type of time-based trigger:
Day timer
- Select time of day:
Midnight to 1am
- Choose which function to run:
- Click “Save”.
- Ensure that the trigger is listed on the Triggers page and is correctly configured to run
deleteOldEmails
daily.
- Go to
View
>Logs
in the script editor to see the logs. - Review the logs for entries indicating the number of threads moved to Trash or if any errors occurred.
- Check the Trash folder in your Gmail to ensure that emails from the Promotions and Social categories, as well as emails older than one week from the Primary category, are being deleted as intended.
- Verify that starred emails and excluded email addresses/labels are preserved.
- If you encounter quota limits or performance issues, adjust the
batchSize
variable in the script to a smaller value.
- Script Errors: If an error occurs, the script logs the error and sends an email notification to the specified address. Check the logs for details.
- Quota Limits: Google imposes limits on the number of emails that can be processed. If you encounter quota issues, consider adjusting the batch size or checking your account's usage limits.
To ensure that specific emails or labels are not deleted, you can configure user-defined exclusions in the script.
In the script, update the excludeEmails
and excludeLabels
arrays as follows:
// User-defined exclusions
var excludeEmails = ['[email protected]', '[email protected]']; // List of email addresses to exclude
var excludeLabels = ['Important', 'Work']; // List of labels to exclude
excludeEmails
: An array of email addresses that should be excluded from deletion.excludeLabels
: An array of labels that should be excluded from deletion.
If you want to exclude emails from [email protected]
and [email protected]
, and keep emails labeled as Family
or Project
, configure the arrays as follows:
var excludeEmails = ['[email protected]', '[email protected]'];
var excludeLabels = ['Family', 'Project'];
This section provides instructions on how to configure the script to exclude specific email addresses and labels, including an example for clarity.
- Testing: Thoroughly test the script to ensure it behaves as expected before relying on it for regular use.
- Manual Review: Periodically review the Trash folder to ensure that only the intended emails are being deleted.
- Script Limitations: Be aware of Google’s quota limits for Gmail and Apps Script.
This README file provides a clear and structured guide to setting up, configuring, and using the auto-deleting email script, including important details about handling errors and considering limitations.