🤖

What to do when "An error has occurred on Google Drive."

2022/05/09に公開

Overview

When creating a large number of files on a shared drive, I encountered an error message "An error has occurred in Google Drive. and the file could not be saved.

The cause of the above may be that the file was caught by the shared drive limitation shown below.

https://support.google.com/a/answer/7338880?hl=en

*The maximum number of items that can be stored on a shared drive
The maximum number of items that can be stored on a shared drive is 400,000. This includes files, folders, and shortcuts. *

*Daily upload limit
Individual users may upload up to 750 GB per day to My Drive and all shared drives. *

If you are caught in the second "daily upload limit," you will have to wait a day.

On the other hand, regarding the first "limit on the number of items that can be stored on a shared drive," you can deal with this by deleting unnecessary files.

However, simply deleting the files will leave them in the Trash and (presumably) will not remove the earlier limit. So, I searched for a script to empty the Trash on the shared drive and came across the following article

https://stackoverflow.com/questions/57764248/is-there-a-script-to-empty-google-team-drive-trash-related-folders

Below is a description of how to use the script presented above. This will allow you to remove the aforementioned limit on the number of items that can be stored on the shared drive when you are caught in the "maximum number of items that can be stored on the shared drive".

How to execute the script to empty the trash on the shared drive

Copy and paste the following script to use.

const driveId = "<shared drive ID>";

function myFunction() { 
  var optionalArgs={driveId, 'includeItemsFromAllDrives':true, 'corpora': 'drive', 'supportsAllDrives': true, 'q':'trashed = true' }
  while(true){
    var trashed=Drive.Files.list(optionalArgs).items;
    console.log("File size to be deleted", trashed.length)
    for(var i=0;i<trashed.length;i++){
      //console.log(i, trashed[i].id)
      try {
        Drive.Files.remove(trashed[i].id, {'supportsAllDrives':true}) 
      } catch (e){
        //console.log({e})
      }
    }
    if(trashed.length == 0){
      break
    }
  }
}

First, go to the following URL

https://script.google.com/home

Then click on "New Project" as shown below.

Copy and paste the previous script. After copying and pasting, change the "driveId" in the first line and click the "Save" button at the top of the screen.

Next, add a service. Go to "Services" and add "Drive API".

Then press the "Execute" button.

You will be asked for approval for the first time.

The following will be deleted 100 at a time as shown below.

After a certain period of time, an error occurs as shown below. In that case, press the "Execute" button again.

Summary

There may be a better solution, but we are happy to help those who are having trouble with "An error has occurred in Google Drive." etc., but we hope this will be helpful to those who are having trouble with this issue.

Postscript.

2022.05.06

I heard that GAS has a 6 minute script execution time limit.

https://developers.google.com/apps-script/guides/services/quotas#current_limitations

Therefore, by running the above script every 5 or 10 minutes, the trash can be emptied automatically.

Specifically, select the trigger and click the "Add Trigger" button as follows.

Then select "Time-Driven," "Minute-Based Timer," or "Every X minutes," as follows.

I hope this was helpful.

Discussion