Deleting files in Google Drive solution

  • 19 June 2023
  • 2 replies
  • 1524 views

Userlevel 2

I use Google Slides and a Google Drive folder on a free Google account to create “course completion certificates” for people who complete training. My zap also emails the certificate and saves it into my database. Since I have limited storage space on the free account and it’s already stored in my database, I don’t want the old certificates just hanging out taking up space. I was looking to make a zap that would delete the certificates or move them to the trash folder. As I was trying to set this up, I learned that it’s not possible using Zapier.

I looked on the forum and found a closed thread called “Deleting files in Google Drive” posted by @Martinewski that had some back and forth with @TheDavidJohnson @jesse @john_fohrman @alex but there was no real solution there. I’ve just recently started tinkering with Google Scripts and I’ve found a solution to the problem. If you create a different folder in Google Drive called something like “Temporary Trash” you can have your zap move the file you want to delete into that folder then use a script to move the file to the trash folder.

To add the script you must open Google Apps Script: You can do this by going to https://script.google.com/. If you're not already logged into your Google account, you will be prompted to do so.

Create a new project: On the Google Apps Script homepage, click on the New project button.

Add the script: In the new window that appears, delete any pre-filled code in the editor, then paste in this script: 

function moveToTrash() {
  // ID of the folder to clear
  var folderId = 'You must insert the temporary trash folder ID here';

  // Get the folder
  var folder = DriveApp.getFolderById(folderId);

  // Get all files in the folder
  var files = folder.getFiles();

  // Loop through all files and move them to trash
  while (files.hasNext()) {
    var file = files.next();
    file.setTrashed(true);
  }
}

You can get the “Temporary Trash” folder ID to copy & paste by opening the folder and copying and pasting (into the script above) everything after https://drive.google.com/drive/folders/the folder id is found here

Save the project: Click on the disk icon or go to File > Save. You will be prompted to give the project a name. This can be anything you like, but something like TrashScript would be appropriate.

Run the script: In order to run the script, you'll first have to give the necessary permissions. Click on the play icon (▶) in the toolbar to run the script. Since this is the first time you're running the script, you will be prompted to authorize the script. Follow the prompts, and make sure to log in with the same account the Google Drive folder belongs to.

Set up a trigger: If you want the script to run automatically, you can set up a trigger. Click on the clock icon in the toolbar, which will open the project triggers page. Click on Add Trigger in the bottom right. For "Choose which function to run," select moveToTrash. For "Select event source", select Time-driven. Then you can customize when and how often you want the trigger to run.

I hope this helps. Best of luck.


This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

2 replies

Userlevel 7
Badge +11

@kmcquait Wow! Thanks so much for taking the time to

  1. come up with this solution
  2. document it so thoroughly
  3. share it here in the Zapier community :) 

How would you feel if we moved this to the Show & Tell section of the community, which is for showing off your Zaps, workarounds, and tricks?

Userlevel 2

Thanks. Yes, I’m ok with moving it.