main-branch/drive_v3

View on GitHub
examples/file_send_to_trash

Summary

Maintainability
Test Coverage
#!/usr/bin/env ruby
# frozen_string_literal: true

# `trash_file` shows how to move a file to the trash.
#
# Moving a file to the trash is done by updating the file's trashed attribute to
# true. The process involves using the update method on the file you wish to move
# to the trash.

require 'drive_v3'

drive_service = DriveV3.drive_service

file_id = ARGV[0] || ENV.fetch('FILE_ID', nil)
raise 'Missing file_id' unless file_id

# Indicate that this application supports files in shared drives. An error will result if
# this is false (or omitted) AND you are trying to update a file in a shared drive.
#
supports_all_drives = true

begin
  print 'Trash file...'
  file_object = { trashed: true }
  file = drive_service.update_file(file_id, file_object, supports_all_drives:)
  puts "update_file result:\n#{file.to_h.pretty_inspect}"
rescue StandardError => e
  puts "An error occurred: #{e.message}"
end