Need to see if your shared folder is taking up space on your dropbox 👨💻? Find out how to check here.
Forum Discussion
D-B
8 years agoExplorer | Level 3
Delete Folder Using PowerShell
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": fals...
Greg-DB
Dropbox Community Moderator
8 years agoUsing 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.)
D-B
8 years agoExplorer | 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 $headersHere 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
- Greg-DB8 years ago
Dropbox Community Moderator
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.
- D-B8 years agoExplorer | 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?
- Greg-DB8 years ago
Dropbox Community Moderator
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
About Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.
The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.
If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X, Facebook or Instagram.
For more info on available support options for your Dropbox plan, see this article.
If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!