How to Automatically Delete Gmail Spam Permanently
This guide walks you through everything from beginning to end, using your Windows computer.
Goal: every 5 minutes, Gmail will automatically check your Spam folder and permanently delete all messages it finds, without moving them to Trash.
- Frequency: every 5 minutes
- Deletion: permanent, no Trash folder
- Target: Gmail Spam folder only
Step 1 — Open Google Apps Script
- Go to Google Apps Script — Create a project.
- Sign in with the Gmail account whose spam you want to delete.
- A new project should open with a code editor.
- Rename
Untitled projecttoGmail Auto Spam Delete.
Step 2 — Paste the corrected code
Inside the editor, you should see a file called Code.gs.
- Select all the existing code using
Ctrl + A. - Delete it.
- Copy and paste the following complete code:
function permanentlyDeleteSpam() {
let totalDeleted = 0;
while (true) {
const result = Gmail.Users.Messages.list("me", {
labelIds: ["SPAM"],
maxResults: 100,
includeSpamTrash: true
});
const messages = result.messages || [];
if (messages.length === 0) break;
Gmail.Users.Messages.batchDelete({
ids: messages.map(message => message.id)
}, "me");
totalDeleted += messages.length;
}
console.log("Permanently deleted: " + totalDeleted);
}
- Click Save (floppy disk icon), or press
Ctrl + S.
This code is the corrected version, including includeSpamTrash: true.
Step 3 — Enable the Gmail API
The script needs permission to communicate with Gmail.
- Look at the left side of Google Apps Script.
- Click Editor if you're not already there.
- Find Services underneath the Files section.
- Click the + next to Services.
- Select Gmail API.
- Choose version v1 if asked.
- Click Add.
For a normal Apps Script project, Google enables the associated Gmail API automatically when you add this service.
Step 4 — Run the script for the first time
This step authorizes the script and performs its first deletion.
- At the top of the editor, find the function dropdown.
- Select
permanentlyDeleteSpam. - Click Run.
- Google may display an authorization window.
- Click Review permissions.
- Select your Google account.
- Review the permissions and click Allow, if you trust the script.
If Google displays an unverified-app warning for your own script, verify that the project and account are yours before proceeding.
After execution, look for the Execution log at the bottom. You should see something like:
Execution started
Permanently deleted: 24
Execution completed
The number will reflect how many messages were processed.
Step 5 — Make it run automatically
Now set up a trigger so you never have to run it yourself again.
- In the left sidebar, click Triggers (clock icon).
- Click + Add Trigger, usually near the bottom right.
- Configure these settings:
| Setting | Select |
|---|---|
| Choose which function to run | permanentlyDeleteSpam |
| Choose which deployment | Head |
| Select event source | Time-driven |
| Select type of time based trigger | Minutes timer |
| Select minute interval | Every 5 minutes |
- Click Save.
Your automation is now configured.
Step 6 — Verify it is working
After the trigger has run, open Executions in Google Apps Script.
You should see successful executions of permanentlyDeleteSpam. You can also check the Spam folder in Gmail to confirm messages have disappeared.
Important things to know
- You don't need to leave your computer on. Google runs the script on its own servers.
- You don't need to keep Gmail open. It works automatically.
- Deletion is irreversible. Any legitimate email mistakenly placed in Spam may be permanently deleted before you can review it.
- It is not instantaneous. Spam can remain for approximately five minutes until the next scheduled run.
Google supports scheduled triggers and permanent deletion through the Gmail API.
Once you have completed all six steps, your Gmail spam-deletion automation is set up.
You can also send a screenshot of any step where you get stuck, and you can pick up from that exact screen.