Start 2025 on time and up to date. Seamlessly integrate your calendars into Dropbox with these simple steps.

Forum Discussion

lucaortis's avatar
lucaortis
Explorer | Level 4
3 years ago

Can't delete files with powershell

 

 

 

function DropBox-FileDelete {

    Param (
        [Parameter(Mandatory=$true)]
        [string]$SourceFilePath
    )

    $DropBoxAccessToken = TOKEN HERE 
    $testFile = $SourceFilePath
    $TargetFilePath="/$testFile"
    $arg = '{"path": "' + $TargetFilePath + '"}'
    
    $authorization = "Bearer " + $DropBoxAccessToken

    $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
    $headers.Add("Authorization", $authorization)
    $headers.Add("Content-Type", 'application/json')
    
    try {
        $result = Invoke-RestMethod -Uri https://api.dropboxapi.com/2/files/delete_v2 -Method Post -Headers $headers -Body $arg
    }
    catch {
        $result = $_.Exception.Response.GetResponseStream()
        $reader = New-Object System.IO.StreamReader($result)
        $reader.BaseStream.Position = 0
        $reader.DiscardBufferedData()
        $responseBody = $reader.ReadToEnd();
    }
    Write-Output $responseBody
}

DropBox-FileDelete "/Applicazioni/Costumer Manager Files/Ore.txt"

 

 

Hello. I just started using the API and I ran into this issue: 
I can upload files, I can download files but I cannot delete them.  

What am I missing here? 
It seems a path problem, but to me the path is fine. 
If the path was wrong, then I would not be able to upload or download, instead, I can download and upload just fine. 

Response body: 

 

 

{"error_summary": "path_lookup/malformed_path/", "error": {".tag": "path_lookup", "path_lookup": {".tag": "malformed_path"}}}

 

 

 

  • Здравко's avatar
    Здравко
    Legendary | Level 20

    lucaortis wrote:

    ...
    It seems a path problem, but to me the path is fine. 
    If the path was wrong, then I would not be able to upload or download, instead, I can download and upload just fine. 

    ...


    Hi lucaortis,

    You haven't posted the code responsible for upload and download. Are you using in the path there double leading slashes too? 🤔😁😉

    Hope this gives direction.

    • lucaortis's avatar
      lucaortis
      Explorer | Level 4

      I found this very confusing but I managed to solve it like this: 

       

       

       $testFile = Split-Path $SourceFilePath -leaf
       $TargetFilePath="/$testFile"
       $arg = '{"path": "' + $TargetFilePath + '"}'

       

       

       

      I know it works, but I'm really confused on how the path itself works. 
      Upload and download code to format PATH are now the same as this. 


      Thank you!!

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Staff rankDropbox Staff

    lucaortis As Здравко pointed out, you have a double leading slash, which is not a valid path format.

     

    Specifically, you're supplying the path "/Applicazioni/Costumer Manager Files/Ore.txt", but then are adding an extra leading slash when you format it with your code:

    $TargetFilePath="/$testFile"

     

About Dropbox API Support & Feedback

Node avatar for Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.5,928 PostsLatest Activity: 12 hours ago
338 Following

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!