Swift
Swift
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 Swift Downloads
func chilkatTest() {
var success: Bool = false
success = 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 gAuth = CkoAuthGoogle()!
gAuth.accessToken = "GOOGLE-DRIVE-ACCESS-TOKEN"
let rest = CkoRest()!
// Connect using TLS.
var bAutoReconnect: Bool = true
success = rest.connect(hostname: "www.googleapis.com", port: 443, tls: true, autoReconnect: bAutoReconnect)
// Provide the authentication credentials (i.e. the access token)
rest.setAuthGoogle(authProvider: gAuth)
// Get 10 results per page for testing. (The default page size is 100, with a max of 1000.
rest.addQueryParam(name: "pageSize", value: "10")
let json = CkoJsonObject()!
var i: Int
var numFiles: Int
// Send the request for the 1st page.
var jsonResponse: String? = rest.fullRequestNoBody(httpVerb: "GET", uriPath: "/drive/v3/files")
var pageNumber: Int = 1
var pageToken: String?
var bContinueLoop: Bool = rest.lastMethodSuccess && (rest.responseStatusCode.intValue == 200)
var fileId: String?
let saFileIds = CkoStringArray()!
while bContinueLoop == true {
print("---- Page \(pageNumber) ----")
json.load(json: jsonResponse)
numFiles = json.size(ofArray: "files").intValue
i = 0
while i < numFiles {
json.i = i
fileId = json.string(of: "files[i].id")
print("name: \(json.string(of: "files[i].name")!)")
print("id: \(fileId!)")
saFileIds.append(str: fileId)
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.
pageToken = json.string(of: "nextPageToken")
bContinueLoop = false
var bHasMorePages: Bool = json.lastMethodSuccess
if bHasMorePages == true {
rest.clearAllQueryParams()
rest.addQueryParam(name: "pageSize", value: "10")
rest.addQueryParam(name: "pageToken", value: pageToken)
jsonResponse = rest.fullRequestNoBody(httpVerb: "GET", uriPath: "/drive/v3/files")
bContinueLoop = rest.lastMethodSuccess && (rest.responseStatusCode.intValue == 200)
pageNumber = pageNumber + 1
}
}
// Before actually deleting, check for errors...
if rest.lastMethodSuccess != true {
print("\(rest.lastErrorText!)")
return
}
// A successful response will have a status code equal to 200.
if rest.responseStatusCode.intValue != 200 {
print("response status code = \(rest.responseStatusCode.intValue)")
print("response status text = \(rest.responseStatusText!)")
print("response header: \(rest.responseHeader!)")
print("response JSON: \(jsonResponse!)")
return
}
// OK, we have the full list of files. Delete each..
let sbPath = CkoStringBuilder()!
numFiles = saFileIds.count.intValue
i = 0
while i < numFiles {
fileId = saFileIds.getString(index: i)
rest.clearAllQueryParams()
sbPath.clear()
sbPath.append(value: "/drive/v3/files/")
sbPath.append(value: fileId)
jsonResponse = rest.fullRequestNoBody(httpVerb: "DELETE", uriPath: sbPath.getAsString())
if rest.lastMethodSuccess != true {
print("\(rest.lastErrorText!)")
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.responseStatusCode.intValue != 204 {
print("response status code = \(rest.responseStatusCode.intValue)")
print("response status text = \(rest.responseStatusText!)")
print("response header: \(rest.responseHeader!)")
print("response JSON: \(jsonResponse!)")
return
}
i = i + 1
print("\(i): Deleted \(fileId!)")
}
print("All Files Deleted.")
}