How to filter out deleted files when retrieving Google Drive file list

I’m working with the Google Drive API v2 to get a list of all files from my drive. However, I need to make sure that deleted files don’t show up in my results.

I attempted to use this API call:

GET https://www.googleapis.com/drive/v2/files?trashed=false

This approach was based on what I found in the API documentation. Unfortunately, I’m still seeing trashed files appearing in my response data. Is there something I’m doing wrong with this parameter, or could this be a known issue with the Drive API? Any suggestions on how to properly exclude these deleted files would be really helpful.

Here’s another workaround that worked for me - do client-side filtering after you get the file list back. Not great for performance, but it’s accurate. When the response comes in, check each file object for labels.trashed and filter out anything where that’s true. I had to use this approach with shared drives where the trashed parameter was all over the place. You can pair this with pagination if you’re dealing with tons of files. The Drive API v2 docs mention this property, though server-side filtering with it isn’t always reliable.

I encountered a similar issue a few months ago. The problem lies with the Drive API v2’s trashed parameter being unpredictable. Instead, I found it more effective to use the q parameter combined with a query string: GET https://www.googleapis.com/drive/v2/files?q=trashed=false. This method proved to be much more reliable. If you’re open to it, transitioning to Drive API v3 is advisable, as it has more consistent filtering capabilities, based on my experience with recent projects.

yeah, this is an annoying bug in the v2 api. add &fields=items(id,title,labels) to your request and manually check the labels.trashed field like dancingbutterfly mentioned. works way better than the buggy trashed parameter. took me weeks to figure this out lol