Copy and Delete

Learn how to perform copy and delete operations using the Dropbox API.

In this lesson, our goal is to explore the endpoints of the file namespace that allow us to copy content from the source to the destination path and delete content. The endpoint operations we’ll focus on are the following:

  • copy_v2
  • delete_v2

Copy a file or a folder

The /files/copy_v2 endpoint lets us copy a file or folder to another location in Dropbox. If the specified source path is a folder, then all of its content will be copied. This endpoint requires us to enable the files.content.write permission.

Request parameters

Let’s see some of the parameters for this call in the table below.

Note: We’ve manually added some files to the TestFolder that we created in the previous lesson so that we may copy them to another folder.

Parameter Name

Description

Format

Required

from_path

This is the path to the Dropbox folder or file that we want to copy.

String

Yes

to_path

This is the destination path to the user's Dropbox.

String

Yes

autorename

This is set to True to allow the Dropbox server to resolve name conflicts.

Boolean

No

Press + to interact
import requests
import json
url = "https://api.dropboxapi.com/2/files/copy_v2"
headers = {
"Authorization": "Bearer {{TOKEN}}",
"Content-Type": "application/json"
}
data = {
"from_path": "/home/TestFolder",
"to_path": "/home/DestFolder",
"autorename": True
}
r = requests.request("POST",url, headers=headers, data=json.dumps(data))
print(json.dumps(r.json(), indent=4))

In the code above, we see the following:

  • Lines 11–15: We show our data to be sent to the endpoint.
  • Lines 12 and 13: We show both the source path and the destination path, respectively. In the destination path, we’ll create a new folder called DestFolder.
  • Line 14: We assign True to autorename. If the /home directory already has a folder named DestFolder, then Dropbox will automatically rename our requested folder.
  • Line 17: This is the HTTP request.
  • Line 18: We print the response.

Response fields

In response to the execution of the code above, we’ll get the .tag, name, path_lower, path_display, and id of our folder or file. Now, in our Dropbox account, we’ll see a new folder was created at the specified path and the content was copied into it.

Delete a file or a folder

The files/delete_v2 endpoint deletes a file or a folder from a specified location. If we provide a path to a folder, then all of its content will also be deleted. It requires us to enable the permission files.content.write to perform this action.

Let’s delete the DestFolder that we created with the copy_v2 endpoint.

Request parameters

Let’s see the request parameters, descriptions, and formats in the table below.

Parameter Name

Description

Format

Required

path

This is the path of the file or folder to be deleted.

String

Yes

parent_rev

This is only applicable to files. A file will only be deleted if the current revisions match the file's most recent revisions.

String

No

Press + to interact
import requests
import json
url = "https://api.dropboxapi.com/2/files/delete_v2"
headers = {
"Authorization": "Bearer {{TOKEN}}",
"Content-Type": "application/json"
}
data = {
"path": "/home/DestFolder"
}
r = requests.request("POST",url, headers=headers, data=json.dumps(data))
print(json.dumps(r.json(), indent=4))

In the code above, we see the following:

  • Lines 11–13: We present our data with the path parameter, which contains the value /home/DestFolder.
  • Line 15: We define the POST request.
  • Line 16: We print the response.

Response fields

Our call’s response will provide the metadata of the deleted folder, which includes the name, path_display, id, and so on.