Skip to content

WebNinjaDeveloper.com

Programming Tutorials




Menu
  • Home
  • Youtube Channel
  • PDF Invoice Generator
Menu

Google Apps Script Deleting Google Drive Files in Folder Example

Posted on May 16, 2023

 

 

BUY FULL SOURCE CODE

 

 

To delete Google Drive files in a specific folder using Google Apps Script, you can follow these steps:

 

 

Step 1: Create a new Google Apps Script project

 

 

Go to the Google Apps Script Editor by visiting script.google.com and create a new project.

 

 

Step 2: Write the code to delete files

 

 

Replace the existing code in the editor with the following code:

 

 

JavaScript
1
2
3
4
5
6
7
8
9
10
11
function deleteFilesInFolder() {
  var folderId = "YOUR_FOLDER_ID"; // Replace with the ID of the folder containing the files to be deleted
  
  var folder = DriveApp.getFolderById(folderId);
  var files = folder.getFiles();
  
  while (files.hasNext()) {
    var file = files.next();
    DriveApp.getFileById(file.getId()).setTrashed(true);
  }
}

 

 

In the code above, replace YOUR_FOLDER_ID with the actual ID of the folder that contains the files you want to delete. The deleteFilesInFolder function retrieves the folder using DriveApp.getFolderById and gets a list of all the files in the folder using folder.getFiles(). It then loops through each file and sets the trashed property to true using setTrashed(true), effectively moving the file to the trash.

 

 

Step 3: Save and run the script

 

 

Save the script and click on the play button to run it. You may be prompted to authorize the script’s permissions. Grant the necessary permissions for the script to access your Google Drive.

 

 

Step 4: Verify the files are deleted

 

 

After running the script, it will delete all the files in the specified folder. You can verify by checking the trash in your Google Drive or by visiting the folder to confirm that the files are no longer present.

 

 

Note: Deleting files permanently from Google Drive using the setTrashed(true) method is irreversible. Be cautious when using this script and ensure that you have backups of any important files.

 

 

BUY FULL SOURCE CODE

 

Recent Posts

  • Python 3 Flask Project to Export Multiple PDF Documents to Word Docx Files Using pdf2docx Library in Browser
  • Build a Fixed Deposit Interest Calculator Using Amount and Time in Browser Using HTML5 & Javascript
  • Python 3 Script to Convert PDF Document to Microsoft Word DOCX File Using pdf2docx Library
  • Node.js Express Project to Remove Background of Images Using Rembg & Formidable Library in Browser
  • Node.js Tutorial to Remove Background From Image Using Rembg & Sharp Library in Command Line
  • Angular
  • Bunjs
  • C#
  • Deno
  • django
  • Electronjs
  • java
  • javascript
  • Koajs
  • kotlin
  • Laravel
  • meteorjs
  • Nestjs
  • Nextjs
  • Nodejs
  • PHP
  • Python
  • React
  • ReactNative
  • Svelte
  • Tutorials
  • Vuejs




©2023 WebNinjaDeveloper.com | Design: Newspaperly WordPress Theme