How to filter out shared files when searching Google Drive API for personal files only?

I’m working with the Google Drive API and need to retrieve only my personal files from the root directory. The problem is that my search results keep including files that others have shared with me, even though I’m trying to exclude them.

Here’s my current approach using Python:

response = drive_service.files().list(
    q="(name contains 'tar' or name contains 'rar') and 'root' in parents and 'me' in owners",
    fields="files(id, name, parents, owners)").execute()
file_list = response.get('files', [])

print('Retrieved Files:')
for file_item in file_list:
    print('{0} - {1} - {2}'.format(file_item['name'], file_item['owners'], file_item['parents']))

Based on the documentation, my query should work correctly. I’m using 'root' in parents to target root folder files and 'me' in owners to get only files I own rather than shared ones.

However, the results still contain entries like this:

archive_2020-03-22-0845.tar [{'kind': 'drive#user', 'displayName': 'SOME OTHER USER', 'me': False, 'permissionId': 'YYYYYYYYYY', 'emailAddress': '[email protected]'}] ['{DIFFERENT_FOLDER_ID']

Notice the 'me': False property and the different parent folder ID. This suggests the file belongs to someone else and isn’t in my actual root folder.

Is there a better way to exclude shared files from the search results? What am I missing in my query parameters?

try adding trashed=false to your query and maybe use 'me' in owners and not sharedWithMe instead. the shared files thing is annoying, i had same issue last month and this helped filter them out better.

The issue you’re experiencing happens because the Google Drive API treats shared files differently than you might expect. When someone shares a file with you that matches your search criteria, it can still appear in results even with the ‘me’ in owners filter. What worked for me was being more explicit about excluding shared content by modifying the query structure. Instead of relying solely on the owners filter, I found success using parents in ‘root’ rather than ‘root’ in parents - this seems to be more restrictive about the actual location. Also consider adding and not sharedWithMe to your query string. While it might seem redundant with the owners filter, the API sometimes interprets these conditions differently and the combination provides better filtering. Another approach that helped in my case was to post-process the results in Python by checking the owners field explicitly and filtering out any files where owner[‘me’] is False. This gives you complete control over what gets included in your final results.

I ran into this exact problem when building a backup tool for my Drive files. The issue is that Google’s API has a quirk where shared files can sometimes bypass the ownership filter depending on how they were shared originally. What solved it for me was implementing a two-step verification process. First, I modified my query to use parents in 'root' instead of 'root' in parents - this is more strict about folder hierarchy. Then I added a secondary check in my code that verifies each file’s owner data explicitly. After getting the initial results, I loop through them and check if file_item['owners'][0]['me'] equals True before adding to my final list. This approach catches edge cases where the API query doesn’t filter properly. The performance hit is minimal since you’re just doing a boolean check on already-retrieved data. Been using this method for about six months now without any false positives from shared files.