I created a little script to delete files in specific folders older than 7 days on Google Drive, and if something is deleted it sends you an email! It doesn’t permanently delete them, it just puts them in the Trashed folder. It works quite well to delete files from Google Drive.
Note: If you’re looking for a backup script that works well with Google Drive, then look no further. I created a backup script that uses Google Drive as the storage solution. Here’s a backup script. It’s only made to work with RunCloud, but can be very easily modified to work on any Linux server.
I currently have a backup solution for my servers whereby my Google Drive structure looks like this:
- Server Backups
- Server 1
- File 1
- File 2
- File 3
- Server 35
- File 1
- File 2
- File 3
- Server 1
The File 1, 2, or 3 can be MySQL backups, application data backups, etc.
Google Apps Script to delete files older than 7 Days in Google Drive
Chrome V8 Engine (Now enabled by default)
function DeleteOldFiles() {
var Folders = new Array(
'FOLDER_ID_HERE',
'FOLDER_ID_HERE'
);
var Files;
Logger.clear();
for (var key in Folders) {
Folder = DriveApp.getFolderById(Folders[key])
Files = Folder.getFiles();
Logger.log('Opening Folder: ' + Folder.getName());
while (Files.hasNext()) {
var File = Files.next();
if (new Date() - File.getLastUpdated() > 7 * 24 * 60 * 60 * 1000) {
File.setTrashed(true); // Places the file in the Trash folder
//Drive.Files.remove(File.getId()); // Permanently deletes the file
Logger.log('File ' + File.getName() + ' was deleted.');
}
}
}
if(Logger.getLog() != '')
MailApp.sendEmail('YOUR_EMAIL_ADDRESS', 'Backups have been removed from Google Drive', Logger.getLog());
}
Old Runtime:
function DeleteOldFiles() {
var Folders = new Array(
'FOLDER_ID_HERE',
'FOLDER_ID_HERE'
);
var Files;
Logger.clear();
for each (var FolderID in Folders) {
Folder = DriveApp.getFolderById(FolderID)
Files = Folder.getFiles();
while (Files.hasNext()) {
var File = Files.next();
if (new Date() - File.getLastUpdated() > 7 * 24 * 60 * 60 * 1000) {
File.setTrashed(true); // Places the file int the Trash folder
//Drive.Files.remove(File.getId()); // Permanently deletes the file
Logger.log('File ' + File.getName() + ' was deleted.');
}
}
}
if(Logger.getLog() != '')
MailApp.sendEmail('YOUR_EMAIL_ADDRESS', 'Backups have been removed from Google Drive', Logger.getLog());
}
To complete the script, and delete files from Google Drive:
- Change
FOLDER_ID_HERE
with the folder ID’s you see in Google Drive. You can get them by double-clicking the folder and the URL structure will be something like this: https://drive.google.com/drive/folders/1Wx81C1hjghjBfgddfgfgmFeMfzdfgHyTdfgkLgdfg3yz
- If you have more folders, just keep adding the ID’s to the Array.
- Change
YOUR_EMAIL_ADDRESS
to your own email address to get notifications. - If you want to permanently delete the files instead of placing them in the Trash folder then un-comment
Drive.Files.remove(File.getId());
- The last part is quite easy and it requires you to make the script run once a day. While the Apps Script editor is open, in the menu click
Edit
->All your triggers
and then set a specific time.
This should work for you:
It's worth noting that Google does not state how it retrieves files, but after some testing it looks like they return files based on the Last Modified with the newest being first. Meaning newer files will always be 'safe' from deletion.
I hope that helps!
Sully
That has worked a treat. I have been looking at this for 6 months just to get it so the files were not completely deleted ( Always retains data )
What would make this complete when running on a schedule is a email reporting,
Fri May 17 13:55:40 BST 2024 INFO: Opening Folder: Call logs : Nothing to be done
Fri May 17 13:55:41 BST 2024 INFO: Opening Folder: Messages : Nothing to be done
Fri May 17 13:55:41 BST 2024 INFO: Opening Folder: Contacts : Nothing to be done
Fri May 17 13:55:42 BST 2024 INFO: Opening Folder: Calendars : Nothing to be done
The script already has reporting in there however, it’s not fully featured. You’ll have to modify this script to your specific needs to return the type of logs you require.
Kind regards,
Sully
I have a surveillance cam which creates a daily folder e.g. 2023-12-12 under the cam folder which is permanent.
I have another sript running which deletes the folder content every 3 days and this works fine.
Unfortunately I have no idea what the issue is. Any ideas?
Additionally I have the following questions:
– I do not need an email to be sent. How can I stop this? Deleting the line results in a script error.
– In this thread you mention the starts_with option. Where exactly must I enter the required name?
Sorry for those questions, but I am not aquainted with scripts 🙁
This script only deletes files.
To stop the email, simply remove the following lines:
Regarding the starts_with feature, within the code, simply replace starts_with with something else, in your case something like this:
How would I fix this?
To disable this, select Run in the menu, then Disable New Apps Script Runtime Powered By Chrome V8.
when it dows not have comment “//”
how to fix this ?
here the screenshot http://i.imgur.com/ZuxBPcn.png
Is there a way to modify the permissions of the folder to allow me to delete any files in a given folder?
Thanks!
At this time I’m not aware of there being a ‘last modified’ function for folders. Currently, all we can get is the time it was created,
How would this script be changed if I wanted to delete a folder 30 days after it’s created date?
The ^ in the script means “starts with”.
When I run the script I get this error:
No item with the given ID could be found, or you do not have permission to access it. (line 12, file “Code”)
I’ve double-checked my folder IDs and I enabled the Drive API under advanced google services as well as in the cloud platform API dashboard, but the error persists. I’ve also given the script the permissions it needs (See, edit, create, and delete all of your Google Drive files).
Any idea what might be causing the error?
What’s your code look like? If it’s line 12, I think one of the Folder ID’s is wrong.
When I run the script I get this error:
No item with the given ID could be found, or you do not have permission to access it. (line 12, file “Code”)
I’ve double-checked my folder IDs and I enabled the Drive API under advanced google services as well as in the cloud platform API dashboard, but the error persists. I’ve also given the script the permissions it needs (See, edit, create, and delete all of your Google Drive files).
Any idea what might be causing the error?
To this:
I’m trying to use this – however, I get an error trying to run this…
”
Missing ) after argument list. (line 3, file “Code”)
”
Infuriating – this looks to do exactly what I need!
Here’s the code I have…
Any thoughts?
Have check valid files for binning (changed to last 7 days), but nothing binned yet.
Will schedule anyway, maybe it takes Drive some time to actually update from this…
Have check valid files for binning (changed to last 7 days), but nothing binned yet.
Will schedule anyway, maybe it takes Drive some time to actually update from this…
“You need to explicitly enable the Advanced Google Services in your Apps Script project AND in the developers console. If you don’t do both, it won’t work.”
Problem is, “Advanced Google Services” is a long list of EVERY API. So far, have enabled Google Drive API (for the file functionality) and GMAIL API (for the notification email).
Still not doing anything, but equally not reporting any errors – Execution Transcript looks to show the script executed successfully…
You are right, the Advanced Google Services needs to be enabled in order to Permanently delete a file. That’s the Drive API. These services must also be enabled in the Google API Console.
Which piece of code are you using? Are you trying to Trash the file (goes to Trash) or are you trying to permanently delete the file?