How to properly remove files from Google Drive using service account and google drive gem

I’m working on a backup management system that uploads files to a Google Drive service account. The problem is that old backup files aren’t getting deleted properly even though the deletion method seems to work.

Here’s my cleanup method:

def cleanup_backup_files
  drive_files = connection.files
  organized = drive_files.sort_by {|file| file.created_time}
  files_to_keep = organized.take(Settings.backup_retention_count)
  files_to_remove = drive_files - files_to_keep
  files_to_remove.each { |file| file.delete(true) }
end

The connection object is set up earlier as my Google Drive session. I get all files with connection.files, sort them by creation date in the organized variable, keep the newest ones in files_to_keep, and put the rest in files_to_remove for deletion.

When I run this in the Rails console, the delete method returns nil which should mean success. I’m even using delete(true) to permanently remove files instead of just trashing them. However, when I check the file count afterward, all the files are still there.

What could be causing the Google Drive gem’s delete method to appear successful but not actually remove the files from the service account?

Been there with backup systems before. Your files array goes stale after the first deletion - when you call connection.files, you get a snapshot, but the gem doesn’t refresh that list after each delete.

Try refetching the file list inside your loop or delete files one by one with fresh API calls. But honestly, this screams automation to me.

I built something similar using Latenode for our backup cleanup. Set up a workflow that hits Google Drive API, grabs files, sorts by date, and nukes the old ones automatically. Best part? Schedule it daily and get alerts when things break.

The visual builder handles all the sorting and “keep X files” logic without wrestling with Ruby code. Plus you get error handling and retries built in.

Perfect for this exact scenario: https://latenode.com

Your delete calls are working but you’re hitting cached data. The Google Drive gem caches file listings, so when you call connection.files at the start, you get a snapshot that won’t update after deletions.

Each file.delete(true) removes the file from Drive, but your files_to_remove array still has stale references. When you check the count afterward, you’re seeing the original cached list.

Fix it by refreshing the connection after each delete or fetching files fresh each time. But honestly, managing Google Drive cleanups gets messy fast with all these edge cases.

I’ve automated this same workflow using Latenode instead of fighting gem quirks. Built a flow that connects to Google Drive API, fetches files, sorts by creation date, keeps the newest X files, and deletes the rest. No caching issues or stale references.

Runs on schedule and alerts me if deletions fail. Way cleaner than debugging Ruby gem behavior and handles API rate limits automatically.

Perfect for backup maintenance like yours: https://latenode.com

Your sorting logic is backwards. You’re using sort_by which gives you oldest files first, then grabbing those with take(). So you’re keeping the oldest files and deleting the newest ones - exactly the opposite of what you want. Fix it by either reversing the sort order to get newest first or swap take() for last() to grab the newest from your current sort. The delete operations work fine, you’re just targeting the wrong files. Had this same issue last year with a backup script. Changed my sort to organized = drive_files.sort_by {|file| file.created_time}.reverse and everything started working. Log the filenames before deleting so you can see what you’re actually targeting.

You’re probably working with file objects instead of file IDs when calling delete. Had this exact issue with a backup cleanup script - the google drive gem gets weird with direct object deletion, especially on file object arrays. Don’t call file.delete(true) on the objects. Extract the file IDs first, then delete by ID: files_to_remove.each { |file| connection.delete_file(file.id) } works way better. Service account permissions might matter too, but I’ve seen ID-based deletion fix most phantom deletion problems. Throw in some logging to check which file IDs you’re targeting before deletion runs.

sounds like a permissions issue. ur service accnt might create files but cnt delete them from that folder. check if it has proper write/delete access or try calling the files.delete API directly instead of the gem’s delete method.

try adding a sleep between deletions. google’s api sometimes needs time to process each delete before moving to the next one. also double-check that your service accnt has domain-wide delegation enabled if you’re using gsuite.