Hello, Guys welcome back to learn How to Get Email Alerts When Files are Deleted in your Drive. Monitor your Google Drive automatically and get email alerts when important files and folders are deleted from your Google Drive. You can also watch file activity in Shared Drives.
When you remove a file on Google Drive, it is moved to the trash folder and remains there eternally until you empty the bin manually. That has always been the case, but Google made a significant adjustment to how the garbage can operates earlier this month.
Files that have been in Google Drive’s trash bin for more than 30 days are automatically removed under the new policy. This automatic cleanup does help regain space, but if you happen to remove some crucial files or folders from your Google Drive, there’s no way to recover them once the 30-day deadline has passed.
Monitor Google Drive Files
If you, like me, are frightened of losing essential files that were accidentally erased, Google Drive Watch can help.
Google Drive Watch is an open-source Google Script that monitors your Google Drive automatically and provides daily email notifications with a complete list of files removed the previous day.
Here’s an example of an email notification sent by Google Drive Watch.
The email contains the file URL, the date the file was created, and the name/email address of the Google Account that most recently updated and deleted the file. It keeps track of files in both your standard Google Drive and Shared Drive folders.
Watch your own Google Drive
Here’s how you can set up Google Drive watch for your own Google account in few easy steps.
- Click here to save the Google script to your Google Drive.
- Go to line #9 of the script editor and enter the email address where you want the Drive notifications to be sent. Multiple emails can also be entered, separated by commas.
- By default, the script will create a cron job that will run once each day at the specified hour. However, if you want to adjust the frequency so that the notifications appear, say, every 5 days, enter 5 in line #10.
- We’re nearly there. To enable the monitor for your Drive, go to the Run menu and select “Enable Drive Watch.” Allow the script access to your file, and you’re done.
Important: The first email notification will only arrive the next day at the selected hour.
How Google Drive Monitoring Works
The source code of the Google Drive monitor script is available on Github.
Internally, the script watches for changes in your Google Drive using the Google Drive API and Google Apps Script. It then creates a daily cron job using Google Scripts triggers to send the email whenever new file modifications are discovered.
When you run the script for the first time, it obtains a starting page token, and all changes made to Google Drive after this token is obtained are tracked by the script. Because the script should monitor folders in Team Drives as well, we set supportsAllDrives to true.
function getPageToken() {
const store = PropertiesService.getScriptProperties();
const token = store.getProperty('token');
if (token) return token;
const { startPageToken } = Drive.Changes.getStartPageToken({
supportsAllDrives: true,
});
store.setProperty('token', startPageToken);
return startPageToken;
}
The Google Drive API’s change.list endpoint retrieves any changes made to the authorised user’s Drive since the start page token. We also set the fields property to limit the number of file properties available in the response. The newStartPageToken returned in the response will be the new page token for future Drive API queries.
How to Download Gmail Messages as EML Files in Google Drive
const fields = `newStartPageToken,
items(file(id,title,labels(trashed),
iconLink,mimeType,createdDate,ownedByMe,
lastModifyingUser(emailAddress,displayName,picture(url)),
alternateLink, fileSize))`;
const { newStartPageToken, items = [] } = Drive.Changes.list({
fields,
pageToken: getPageToken(),
includeItemsFromAllDrives: true,
pageSize: 100,
supportsAllDrives: true,
});
if (newStartPageToken) {
propertyStore.setProperty('token', newStartPageToken);
}
The array items contains a list of files that have been modified since the last execution. This contains both new files that have been added and old ones that have been changed by users. Because we are only interested in destroyed files, we will filter all files from the response except those that have been trashed.
How to Generate firebase.json file for Firebase Redirects
const filteredItems = items
.map(({ file }) => file)
// Only interested in files where "I" am the owner
.filter(({ ownedByMe }) => ownedByMe)
// Only interested in files that have been "trashed"
.filter(({ labels: { trashed } = {} }) => trashed === true)
// Only return fields that are sent by email
.map((file) => {
const {
iconLink,
alternateLink,
title,
lastModifyingUser = {},
createdDate,
fileSize,
} = file;
return { iconLink, alternateLink, title, createdDate, fileSize };
});
We can utilise the Gmail service to tell the user now that we have an array of files that the user has removed since the last run.