Swift Dropbox: Dropbox Delete a File

Back to Index

Deletes a file on Dropbox

Documentation: https://www.dropbox.com/developers/documentation/http/documentation#files-delete

CURL Command

curl -X POST https://api.dropboxapi.com/2/files/delete_v2 \
    --header "Authorization: Bearer DROPBOX-ACCESS-TOKEN" \
    --header "Content-Type: application/json" \
    --data "{\"path\": \"/Homework/math/Prime_Numbers.txt\"}"

Swift Example


func chilkatTest() {
    let rest = CkoRest()
    var success: Bool

    //  URL: https://api.dropboxapi.com/2/files/delete_v2
    var bTls: Bool = true
    var port: Int = 443
    var bAutoReconnect: Bool = true
    success = rest.Connect("api.dropboxapi.com", port: port, tls: bTls, autoReconnect: bAutoReconnect)
    if success != true {
        print("ConnectFailReason: \(rest.ConnectFailReason.intValue)")
        print("\(rest.LastErrorText)")
        return
    }

    //  See the Online Tool for Generating JSON Creation Code
    let json = CkoJsonObject()
    json.UpdateString("path", value: "/Homework/math/Prime_Numbers.txt")

    rest.AddHeader("Authorization", value: "Bearer DROPBOX-ACCESS-TOKEN")
    rest.AddHeader("Content-Type", value: "application/json")

    let sbRequestBody = CkoStringBuilder()
    json.EmitSb(sbRequestBody)
    let sbResponseBody = CkoStringBuilder()
    success = rest.FullRequestSb("POST", uriPath: "/2/files/delete_v2", requestBody: sbRequestBody, responseBody: sbResponseBody)
    if success != true {
        print("\(rest.LastErrorText)")
        return
    }

    var respStatusCode: Int = rest.ResponseStatusCode.intValue
    if respStatusCode >= 400 {
        print("Response Status Code = \(respStatusCode)")
        print("Response Header:")
        print("\(rest.ResponseHeader)")
        print("Response Body:")
        print("\(sbResponseBody.GetAsString())")
        return
    }

    let jsonResponse = CkoJsonObject()
    jsonResponse.LoadSb(sbResponseBody)

    //  See the Online Tool for Generating JSON Parse Code
    var i: Int
    var count_i: Int
    var j: Int
    var count_j: Int

    var metadataTag: String? = jsonResponse.StringOf("metadata.\".tag\"")
    var metadataName: String? = jsonResponse.StringOf("metadata.name")
    var metadataId: String? = jsonResponse.StringOf("metadata.id")
    var metadataClient_modified: String? = jsonResponse.StringOf("metadata.client_modified")
    var metadataServer_modified: String? = jsonResponse.StringOf("metadata.server_modified")
    var metadataRev: String? = jsonResponse.StringOf("metadata.rev")
    var metadataSize: Int = jsonResponse.IntOf("metadata.size").intValue
    var metadataPath_lower: String? = jsonResponse.StringOf("metadata.path_lower")
    var metadataPath_display: String? = jsonResponse.StringOf("metadata.path_display")
    var metadataSharing_infoRead_only: Bool = jsonResponse.BoolOf("metadata.sharing_info.read_only")
    var metadataSharing_infoParent_shared_folder_id: String? = jsonResponse.StringOf("metadata.sharing_info.parent_shared_folder_id")
    var metadataSharing_infoModified_by: String? = jsonResponse.StringOf("metadata.sharing_info.modified_by")
    var metadataHas_explicit_shared_members: Bool = jsonResponse.BoolOf("metadata.has_explicit_shared_members")
    var metadataContent_hash: String? = jsonResponse.StringOf("metadata.content_hash")
    i = 0
    count_i = jsonResponse.SizeOfArray("metadata.property_groups").intValue
    while i < count_i {
        jsonResponse.I = i
        var template_id: String? = jsonResponse.StringOf("metadata.property_groups[i].template_id")
        j = 0
        count_j = jsonResponse.SizeOfArray("metadata.property_groups[i].fields").intValue
        while j < count_j {
            jsonResponse.J = j
            var name: String? = jsonResponse.StringOf("metadata.property_groups[i].fields[j].name")
            var value: String? = jsonResponse.StringOf("metadata.property_groups[i].fields[j].value")
            j = j + 1
        }

        i = i + 1
    }


}

Sample JSON Response Body

{
  "metadata": {
    ".tag": "file",
    "name": "Prime_Numbers.txt",
    "id": "id:a4ayc_80_OEAAAAAAAAAXw",
    "client_modified": "2015-05-12T15:50:38Z",
    "server_modified": "2015-05-12T15:50:38Z",
    "rev": "a1c10ce0dd78",
    "size": 7212,
    "path_lower": "/homework/math/prime_numbers.txt",
    "path_display": "/Homework/math/Prime_Numbers.txt",
    "sharing_info": {
      "read_only": true,
      "parent_shared_folder_id": "84528192421",
      "modified_by": "dbid:AAH4f99T0taONIb-OurWxbNQ6ywGRopQngc"
    },
    "property_groups": [
      {
        "template_id": "ptid:1a5n2i6d3OYEAAAAAAAAAYa",
        "fields": [
          {
            "name": "Security Policy",
            "value": "Confidential"
          }
        ]
      }
    ],
    "has_explicit_shared_members": false,
    "content_hash": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
  }
}