Need to see if your shared folder is taking up space on your dropbox 👨‍💻? Find out how to check here.

Forum Discussion

D-B's avatar
D-B
Explorer | Level 3
8 years ago

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.

 

 

9 Replies

Replies have been turned off for this discussion
  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    8 years ago

    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-B's avatar
    D-B
    Explorer | Level 3
    8 years ago

    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's avatar
    Greg-DB
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    8 years ago

    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-B's avatar
    D-B
    Explorer | Level 3
    8 years ago

    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-DB's avatar
    Greg-DB
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    8 years ago

    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

     

  • D-B's avatar
    D-B
    Explorer | Level 3
    8 years ago

    I get output now, thanks.

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

     

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    8 years ago

    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".

  • D-B's avatar
    D-B
    Explorer | Level 3
    8 years ago

    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
  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    8 years ago

    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.

About Dropbox API Support & Feedback

Node avatar for 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!