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

  1. Go to Google Apps Script — Create a project.
  2. Sign in with the Gmail account whose spam you want to delete.
  3. A new project should open with a code editor.
  4. Rename Untitled project to Gmail Auto Spam Delete.

Step 2 — Paste the corrected code

Inside the editor, you should see a file called Code.gs.

  1. Select all the existing code using Ctrl + A.
  2. Delete it.
  3. 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);
}
  1. 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.

  1. Look at the left side of Google Apps Script.
  2. Click Editor if you're not already there.
  3. Find Services underneath the Files section.
  4. Click the + next to Services.
  5. Select Gmail API.
  6. Choose version v1 if asked.
  7. 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.

  1. At the top of the editor, find the function dropdown.
  2. Select permanentlyDeleteSpam.
  3. Click Run.
  4. Google may display an authorization window.
  5. Click Review permissions.
  6. Select your Google account.
  7. 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.

  1. In the left sidebar, click Triggers (clock icon).
  2. Click + Add Trigger, usually near the bottom right.
  3. 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
  1. 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.