Java 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\"}"

Java Example

import com.chilkatsoft.*;

public class ChilkatExample {

  static {
    try {
        System.loadLibrary("chilkat");
    } catch (UnsatisfiedLinkError e) {
      System.err.println("Native code library failed to load.\n" + e);
      System.exit(1);
    }
  }

  public static void main(String argv[])
  {
    CkRest rest = new CkRest();
    boolean success;

    //  URL: https://api.dropboxapi.com/2/files/delete_v2
    boolean bTls = true;
    int port = 443;
    boolean bAutoReconnect = true;
    success = rest.Connect("api.dropboxapi.com",port,bTls,bAutoReconnect);
    if (success != true) {
        System.out.println("ConnectFailReason: " + rest.get_ConnectFailReason());
        System.out.println(rest.lastErrorText());
        return;
        }

    //  See the Online Tool for Generating JSON Creation Code
    CkJsonObject json = new CkJsonObject();
    json.UpdateString("path","/Homework/math/Prime_Numbers.txt");

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

    CkStringBuilder sbRequestBody = new CkStringBuilder();
    json.EmitSb(sbRequestBody);
    CkStringBuilder sbResponseBody = new CkStringBuilder();
    success = rest.FullRequestSb("POST","/2/files/delete_v2",sbRequestBody,sbResponseBody);
    if (success != true) {
        System.out.println(rest.lastErrorText());
        return;
        }

    int respStatusCode = rest.get_ResponseStatusCode();
    if (respStatusCode >= 400) {
        System.out.println("Response Status Code = " + respStatusCode);
        System.out.println("Response Header:");
        System.out.println(rest.responseHeader());
        System.out.println("Response Body:");
        System.out.println(sbResponseBody.getAsString());
        return;
        }

    CkJsonObject jsonResponse = new CkJsonObject();
    jsonResponse.LoadSb(sbResponseBody);

    //  See the Online Tool for Generating JSON Parse Code
    int i;
    int count_i;
    int j;
    int count_j;

    String metadataTag = jsonResponse.stringOf("metadata.\".tag\"");
    String metadataName = jsonResponse.stringOf("metadata.name");
    String metadataId = jsonResponse.stringOf("metadata.id");
    String metadataClient_modified = jsonResponse.stringOf("metadata.client_modified");
    String metadataServer_modified = jsonResponse.stringOf("metadata.server_modified");
    String metadataRev = jsonResponse.stringOf("metadata.rev");
    int metadataSize = jsonResponse.IntOf("metadata.size");
    String metadataPath_lower = jsonResponse.stringOf("metadata.path_lower");
    String metadataPath_display = jsonResponse.stringOf("metadata.path_display");
    boolean metadataSharing_infoRead_only = jsonResponse.BoolOf("metadata.sharing_info.read_only");
    String metadataSharing_infoParent_shared_folder_id = jsonResponse.stringOf("metadata.sharing_info.parent_shared_folder_id");
    String metadataSharing_infoModified_by = jsonResponse.stringOf("metadata.sharing_info.modified_by");
    boolean metadataHas_explicit_shared_members = jsonResponse.BoolOf("metadata.has_explicit_shared_members");
    String metadataContent_hash = jsonResponse.stringOf("metadata.content_hash");
    i = 0;
    count_i = jsonResponse.SizeOfArray("metadata.property_groups");
    while (i < count_i) {
        jsonResponse.put_I(i);
        String template_id = jsonResponse.stringOf("metadata.property_groups[i].template_id");
        j = 0;
        count_j = jsonResponse.SizeOfArray("metadata.property_groups[i].fields");
        while (j < count_j) {
            jsonResponse.put_J(j);
            String name = jsonResponse.stringOf("metadata.property_groups[i].fields[j].name");
            String value = 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"
  }
}