javaspring-bootgoogle-drive-apigoogle-api-java-clientgoogle-account

Why deleted file still show up in result while query by Google Drive API


I am doing a service automatic generate file and upload it to google drive using Spring boot and java 8. This is an external service of another bigger service.

The Google environment I'm using:

There was some file and folder inside folder reports that I was deleted normally by UI using google account A (remove then empty trash). While I'm query using service account key C, those file and folder that I was deleted still show up in the result. I have wait 1 day but the result still contain deleted file.

Can somebody explain for me about this behavior of Google Drive API?

Here are the code :

public List<GoogleFileItem> getAllFile() {
    try {
        if (!serviceAccountKey.exists()) {
            throw new Exception("key file don't exist");
        }
        Drive drive = createDrive();

        List<GoogleFileItem> responseList = new ArrayList<>();

        FileList result = drive.files().list()
                .setQ("trashed=false")
                .setFields("nextPageToken, files(id, name, kind, mimeType, parents, trashed)")
                .execute();
        List<File> files = result.getFiles();
        int i =0 ;
        for (File file : files) {
            if (file.getTrashed()) i++;
            GoogleFileItem item = new GoogleFileItem();
            item.setId(file.getId());
            item.setName(file.getName());
            item.setThumbnailLink(file.getThumbnailLink());
            item.setKind(file.getKind());
            item.setMimeType(GoogleMimeType.find(file.getMimeType()));
            if (file.getParents() != null) {
                item.setParents(file.getParents());
            } else {
                item.setParents(Collections.singletonList(""));
            }
            responseList.add(item);
        }

        return responseList;

    } catch (Exception e) {
        e.printStackTrace();
        logger.error("Exception: " + e);
        return null;
    }
}

Edit:


Solution

  • Answer:

    If a file in a shared folder is deleted by a collaborator (not the owner), it's only deleted for that collaborator, and not for the others. According to the docs, only the owner would have access to this file, but other collaborators (which haven't actively deleted that file) seem to have access too:

    When someone deletes a file from a shared folder, only the owner can access it.

    Because of this, when a collaborator (not owner) tries to remove a file from a shared folder in the UI, the following message is displayed:

    "{YOUR_FILE_NAME}" will be removed from view. Collaborators will still have access.

    Because the file was uploaded by the service account, this account is the file owner. Even if a collaborator (your regular account) deletes it, the service account (being the owner) can still access it.

    Reference: