When does the files get deleted from Google Drive

2019-09-15 06:29发布

问题:

I wanted to know how long does a file in google drive service account stay?

I couldn't find its default expiration time. As I don't have control over the account, how can I know the file is deleted or if no way, Do I have to delete the files from service account myself?

回答1:

since you don't have control over the account, I don't see how you can actually delete the files owned by that account. the file will stay there as long as the owner not deleting it.

https://support.google.com/drive/answer/2375102

Put a file in trash

To remove a file from your Drive, you can put it in your trash. Your file will stay there until you empty your trash.

If you're the owner of the file, others can view it until you permanently delete the file. If you're not the owner, others can see the file even if you empty your trash.



回答2:

It is the same how a user creates a file (no expiration date). To know any changes like if it is deleted, you can check the Change Feed to retrieve the changes.

Changes: list

  • Lists the changes for a user.

Detect Changes

  • For Google Drive apps that need to keep track of changes to files, the Changes collection provides an efficient way to detect changes to all files, including those that have been shared with a user. The collection works by providing the current state of each file, if and only if the file has changed since a given point in time.

You can delete the files using the Service Account given that it is the owner of the file. You can also check this SO question regarding how to check if a file/folder is deleted.



回答3:

The files are there to stay until you delete them yourself. You can always get the file list from the google drive API (I use V3) and get the files list with their ID and delete whichever you want. You can't yet do it via web interface.

Below is an example is c# on how to get the files list:

    public List<Google.Apis.Drive.v3.Data.File> GetFilesList(string SearchQ = null)
    {
        List<Google.Apis.Drive.v3.Data.File> ListOfFiles = new List<Google.Apis.Drive.v3.Data.File>();

        FilesResource.ListRequest listRequest = _CurrentDriveService.Files.List();
        listRequest.PageSize = 1000;
        if (SearchQ != null)
        {
            listRequest.Q = SearchQ; //("'PARENT_ID' in parents");
        }
        listRequest.Fields = "nextPageToken, files(id, name,parents,mimeType,size,capabilities,modifiedTime,webViewLink,webContentLink)";
        FileList fileFeedList = listRequest.Execute();

        while (fileFeedList != null)
        {
            foreach (File file in fileFeedList.Files)
            {
                ListOfFiles.Add(file);
            }

            if (fileFeedList.NextPageToken == null)
            {
                break;
            }

            listRequest.PageToken = fileFeedList.NextPageToken;

            fileFeedList = listRequest.Execute();

        }
        return ListOfFiles;

    }

and how to delete a particular file:

    public bool DeleteFileFromDrive(string FileID) // only to be used when you are the owner of the file. Otherwise it will have no effect.
    {
        try
        {

            FilesResource.GetRequest gr = new FilesResource.GetRequest(_CurrentDriveService, FileID);
            var FileData = gr.Execute();
            if (FileData.MimeType == GoogleDriveMimeTypes.GetFolderMimeTypeString())
            {
                var filesInFolder = GetFilesList("'" + FileID + "' in parents");
                if (filesInFolder.Count > 0)
                {
                    // directory is not empty
                    return false;
                }
            }

            var requestD = _CurrentDriveService.Files.Delete(FileID);

            requestD.Execute();
            return true;
        }
        catch (Exception e)
        {
            System.Diagnostics.Debug.WriteLine(e.Message);
            return false;
        }

    }


回答4:

What I figured out was that if you delete the file from service account using API(and because service account is the owner of that file), it will be deleted from all the Shared Drive Sections of the users who access the file. Thanks @some1, @MrRebot and @Hadar Ben David for helping me come up with this conclusion.