One month down in 2025: How are your resolutions coming along? Check out how to get back on track here.
Forum Discussion
D-B
7 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": 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.
- Greg-DB
Dropbox Staff
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.)
- D-BExplorer | 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
- Greg-DB
Dropbox Staff
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.
About Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.5,942 PostsLatest Activity: 4 hours ago
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 or Facebook.
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!