Rust
Rust
Delete All Files
See more Google Drive Examples
Permanently deletes all files owned by the user without moving it to the trash.This example works by first getting a list of all fileIds, and then iterating over the list to delete each file.
See Google Drive Files delete for more information.
Chilkat Rust Downloads
let _ = true;
// It requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// This example uses a previously obtained access token having permission for the
// Google Drive scope.
let g_auth = chilkat::AuthGoogle::new();
g_auth.set_access_token("GOOGLE-DRIVE-ACCESS-TOKEN");
let rest = chilkat::Rest::new();
// Connect using TLS.
let b_auto_reconnect = true;
let _ = rest.connect("www.googleapis.com", 443, true, b_auto_reconnect).is_ok();
// Provide the authentication credentials (i.e. the access token)
let _ = rest.set_auth_google(&g_auth);
// Get 10 results per page for testing. (The default page size is 100, with a max of 1000.
let _ = rest.add_query_param("pageSize", "10");
let json = chilkat::JsonObject::new();
let mut i: i32 = 0;
let mut num_files: i32 = 0;
// Send the request for the 1st page.
let mut json_response = rest.full_request_no_body("GET", "/drive/v3/files").unwrap_or_default();
let mut page_number = 1;
let mut page_token = String::new();
let mut b_continue_loop = rest.last_method_success() && (rest.response_status_code() == 200);
let mut file_id = String::new();
let sa_file_ids = chilkat::StringArray::new();
while b_continue_loop {
println!("---- Page {} ----", page_number);
let _ = json.load(&json_response);
num_files = json.size_of_array("files");
i = 0;
while i < num_files {
json.set_i(i);
file_id = json.string_of("files[i].id").unwrap_or_default();
println!("name: {}", json.string_of("files[i].name").unwrap_or_default());
println!("id: {}", file_id);
let _ = sa_file_ids.append(&file_id);
i = i + 1;
}
// Get the next page of files.
// If the "nextPageToken" is present in the JSON response, then use it in the "pageToken" parameter
// for the next request. If no "nextPageToken" was present, then this was the last page of files.
page_token = json.string_of("nextPageToken").unwrap_or_default();
b_continue_loop = false;
let b_has_more_pages = json.last_method_success();
if b_has_more_pages {
let _ = rest.clear_all_query_params();
let _ = rest.add_query_param("pageSize", "10");
let _ = rest.add_query_param("pageToken", &page_token);
json_response = rest.full_request_no_body("GET", "/drive/v3/files").unwrap_or_default();
b_continue_loop = rest.last_method_success() && (rest.response_status_code() == 200);
page_number = page_number + 1;
}
}
// Before actually deleting, check for errors...
if !rest.last_method_success() {
println!("{}", rest.last_error_text());
return;
}
// A successful response will have a status code equal to 200.
if rest.response_status_code() != 200 {
println!("response status code = {}", rest.response_status_code());
println!("response status text = {}", rest.response_status_text());
println!("response header: {}", rest.response_header());
println!("response JSON: {}", json_response);
return;
}
// OK, we have the full list of files. Delete each..
let sb_path = chilkat::StringBuilder::new();
num_files = sa_file_ids.count();
i = 0;
while i < num_files {
file_id = sa_file_ids.get_string(i).unwrap_or_default();
let _ = rest.clear_all_query_params();
sb_path.clear();
let _ = sb_path.append("/drive/v3/files/");
let _ = sb_path.append(&file_id);
json_response = rest.full_request_no_body("DELETE", &sb_path.get_as_string().unwrap_or_default()).unwrap_or_default();
if !rest.last_method_success() {
println!("{}", rest.last_error_text());
return;
}
// A successful response will have a status code equal to 204 and the response body is empty.
// (If not successful, then there should be a JSON response body with information..)
if rest.response_status_code() != 204 {
println!("response status code = {}", rest.response_status_code());
println!("response status text = {}", rest.response_status_text());
println!("response header: {}", rest.response_header());
println!("response JSON: {}", json_response);
return;
}
i = i + 1;
println!("{}: Deleted {}", i, file_id);
}
println!("All Files Deleted.");