cancel
Showing results for 
Search instead for 
Did you mean: 
Announcements
Update: Find information on Dropbox support during COVID-19 here
Close
cancel
Showing results for 
Search instead for 
Did you mean: 

Delete Folder Using PowerShell

D-B
Explorer | Level 3

I can't figure out how to delete a file in my Dropbox using Powershell. I can upload fine using this :

 

$arg = '{ "path": "' + $TargetFilePath + '", "mode": "add", "autorename": true, "mute": false }'
$authorization = "Bearer " + $DropBoxAccessToken

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", $authorization)
$headers.Add("Dropbox-API-Arg", $arg)
$headers.Add("Content-Type", 'application/octet-stream')

Invoke-RestMethod -Uri https://content.dropboxapi.com/2/files/upload -Method Post -InFile $SourceFilePath -Headers $headers

But I don't now how to use "https://api.dropboxapi.com/2/files/delete_v2" even after reading the documentation (I'm not a developer so this is new to me).

I used the Dropbox API Explorer • delete_v2 to generate the code for a curl request which works from the Dropbox API Explorer but I'd like to use PowerShell in the same way I do for uploading. The working curl request is:

curl -X POST https://api.dropboxapi.com/2/files/delete_v2 \
  --header 'Authorization: Bearer <access-token>' \
  --header 'Content-Type: application/json' \
  --data '{"path":"/20181009"}'

Can anyone tell me how to use "Invoke-RestMethod -Uri https://api.dropboxapi.com/2/files/delete_v2" in PowerShell similar to the upload method above to delete a folder? Thanks.

 

 

9 Replies 9

Re: Delete Folder Using PowerShell

Dropboxer
Dropboxer

Using the /2/files/delete_v2 endpoint is the right way to delete a folder. Calling it will be similar to /2/files/upload, but with some important differences. 

The /2/files/upload endpoint is a "content-upload" endpoint, so it takes the parameters as JSON in the "Dropbox-API-Arg" header.

The /2/files/delete_v2 endpoint is an "RPC" endpoint though, so it takes the parameters as JSON in the request body.

If you have code for /2/files/delete_v2 that doesn't seem to be working, print out the response body you get when you call it. If the call isn't working, there should be an error message in the response body. (You may need to add some extra code to print that out, e.g., as shown in this post.)

Re: Delete Folder Using PowerShell

D-B
Explorer | Level 3

Thank you for your reply, sorry that I don't understand everything you have explained here, this is the first time I've attempted anything like this.

 

The best I could do is to take the working upload code and change it to the below which isn't correct but the closest I could get to what I thought it should be. It seems that my Invoke-RestMethod command parameters are wrong and probably the headers too.

 

 

$TargetFilePath = "/20181008"
$DropBoxAccessToken = "<access-token>"

$arg = '{ "path": "' + $TargetFilePath + '" }'
$authorization = "Bearer " + $DropBoxAccessToken
 
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", $authorization)
$headers.Add("Dropbox-API-Arg", $arg)
$headers.Add("Content-Type", 'application/json')
 
Invoke-RestMethod -Method Delete -Uri https://api.dropboxapi.com/2/files/delete_v2 -Headers $headers

Here is the output after the PowerShell script fails:

Invoke-RestMethod : The remote server returned an error: (400) Bad Request.
At (SCRIPT LOCATION)\Dropbox-Delete.ps1:12 char:1
+ Invoke-RestMethod -Method Delete -Uri https://api.dropboxapi.com/2/fi ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebExc
   eption
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

 

 

Re: Delete Folder Using PowerShell

Dropboxer
Dropboxer

Can you print the response body as mentioned/linked in my last post? You'll want to retrieve the error message from it to debug things like this.

Re: Delete Folder Using PowerShell

D-B
Explorer | Level 3

I just tried adding the code from each answer in that post but I get no output from PowerShell at all.

First one I tried:

$TargetFilePath = "/20181008"
$DropBoxAccessToken = "<access-token>"

$arg = '{ "path": "' + $TargetFilePath + '" }'
$authorization = "Bearer " + $DropBoxAccessToken
 
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", $authorization)
$headers.Add("Dropbox-API-Arg", $arg)
$headers.Add("Content-Type", 'application/json')
 
$resp = try { Invoke-RestMethod -Method Delete -Uri https://api.dropboxapi.com/2/files/delete_v2 -Headers $headers } catch { $_.Exception.Response }

Second:

$TargetFilePath = "/20181008"
$DropBoxAccessToken = "<access-token>"

$arg = '{ "path": "' + $TargetFilePath + '" }'
$authorization = "Bearer " + $DropBoxAccessToken
 
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", $authorization)
$headers.Add("Dropbox-API-Arg", $arg)
$headers.Add("Content-Type", 'application/json')

try {
        $result = Invoke-RestMethod -Method Delete -Uri https://api.dropboxapi.com/2/files/delete_v2 -Headers $headers
}
catch {
        $result = $_.Exception.Response.GetResponseStream()
        $reader = New-Object System.IO.StreamReader($result)
        $reader.BaseStream.Position = 0
        $reader.DiscardBufferedData()
        $responseBody = $reader.ReadToEnd();
}

I must be formatting this wrong?

Re: Delete Folder Using PowerShell

Dropboxer
Dropboxer

I believe your second version is closer, as it actually reads the body. (The first one doesn't appear to.)

In both though, you're not actually printing out the result or saving it anyway. E.g., something like:

 

Write-Output $responseBody

 

Re: Delete Folder Using PowerShell

D-B
Explorer | Level 3

I get output now, thanks.

Your request's HTTP request method is "DELETE", which is not supported by the Dropbox API v2.

 

Re: Delete Folder Using PowerShell

Dropboxer
Dropboxer

That output indicates that the issue is that you're using the "DELETE" method, which isn't allowed. The API expect the "POST" method. 

That's presumably due to the "-Method Delete" in your code; for POST you would likely need to set "-Method Post".

Re: Delete Folder Using PowerShell

D-B
Explorer | Level 3

This is the response when I changed Delete to Post.

Error in call to API function "files/delete:2": Unexpected HTTP headers: "Dropbox-Api-Arg"

When removing the line $headers.Add("Dropbox-API-Arg", $arg) the response is:

Error in call to API function "files/delete:2": request body: could not decode input as JSON

Re: Delete Folder Using PowerShell

Dropboxer
Dropboxer

That is also expected, as this endpoint requires the parameters be sent as JSON in the request body. Please refer to my earlier comment for links to documentation for this.

Work Smarter with Dropbox

The way we work is changing. Share and discover new ways to work smarter with Dropbox in our community.

Sound good? Let's get started.
Who's talking

Top contributors to this post

What do Dropbox user levels mean?
Need more support?