Sample code for 30+ languages & platforms
Zig

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 Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

    // 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.

    const g_auth = try chilkat.AuthGoogle.init();
    defer g_auth.deinit();
    g_auth.setAccessToken("GOOGLE-DRIVE-ACCESS-TOKEN");

    const rest = try chilkat.Rest.init();
    defer rest.deinit();

    // Connect using TLS.
    const b_auto_reconnect = true;
    try rest.connect("www.googleapis.com", 443, true, b_auto_reconnect);

    // Provide the authentication credentials (i.e. the access token)
    rest.setAuthGoogle(g_auth) catch {};

    // Get 10 results per page for testing.  (The default page size is 100, with a max of 1000.
    rest.addQueryParam("pageSize", "10") catch {};

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    var i: i32 = 0;
    var num_files: i32 = 0;

    // Send the request for the 1st page.
    var json_response: [:0]const u8 = try rest.fullRequestNoBody(alloc, "GET", "/drive/v3/files");

    var page_number: i32 = 1;
    var page_token: [:0]const u8 = "";
    var b_continue_loop: bool = rest.getLastMethodSuccess() and (rest.getResponseStatusCode() == 200);

    var file_id: [:0]const u8 = "";
    const sa_file_ids = try chilkat.StringArray.init();
    defer sa_file_ids.deinit();

    while (b_continue_loop) {
        std.debug.print("---- Page {d} ----\n", .{page_number});
        json.load(json_response) catch {};

        num_files = json.sizeOfArray("files");
        i = 0;
        while (i < num_files) {
            json.setI(i);
            file_id = try json.stringOf(alloc, "files[i].id");
            std.debug.print("name: {s}\n", .{try json.stringOf(alloc, "files[i].name")});
            std.debug.print("id: {s}\n", .{file_id});
            sa_file_ids.append(file_id) catch {};
            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 = try json.stringOf(alloc, "nextPageToken");
        b_continue_loop = false;
        const b_has_more_pages = json.getLastMethodSuccess();
        if (b_has_more_pages) {
            rest.clearAllQueryParams() catch {};
            rest.addQueryParam("pageSize", "10") catch {};
            rest.addQueryParam("pageToken", page_token) catch {};
            json_response = try rest.fullRequestNoBody(alloc, "GET", "/drive/v3/files");
            b_continue_loop = rest.getLastMethodSuccess() and (rest.getResponseStatusCode() == 200);
            page_number = page_number + 1;
        }
    }

    // Before actually deleting, check for errors...
    if (!rest.getLastMethodSuccess()) {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    }

    // A successful response will have a status code equal to 200.
    if (rest.getResponseStatusCode() != 200) {
        std.debug.print("response status code = {d}\n", .{rest.getResponseStatusCode()});
        std.debug.print("response status text = {s}\n", .{try rest.getResponseStatusText(alloc)});
        std.debug.print("response header: {s}\n", .{try rest.getResponseHeader(alloc)});
        std.debug.print("response JSON: {s}\n", .{json_response});
        return;
    }

    // OK, we have the full list of files.  Delete each..
    const sb_path = try chilkat.StringBuilder.init();
    defer sb_path.deinit();
    num_files = sa_file_ids.getCount();
    i = 0;
    while (i < num_files) {
        file_id = try sa_file_ids.getString(alloc, i);

        rest.clearAllQueryParams() catch {};

        sb_path.clear();
        sb_path.append("/drive/v3/files/") catch {};
        sb_path.append(file_id) catch {};

        json_response = rest.fullRequestNoBody(alloc, "DELETE", try sb_path.getAsString(alloc)) catch {
            std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
            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.getResponseStatusCode() != 204) {
            std.debug.print("response status code = {d}\n", .{rest.getResponseStatusCode()});
            std.debug.print("response status text = {s}\n", .{try rest.getResponseStatusText(alloc)});
            std.debug.print("response header: {s}\n", .{try rest.getResponseHeader(alloc)});
            std.debug.print("response JSON: {s}\n", .{json_response});
            return;
        }

        i = i + 1;
        std.debug.print("{d}: Deleted {s}\n", .{ i, file_id });
    }

    std.debug.print("All Files Deleted.\n", .{});
}