Search code examples
pythonowncloud

How to delete file in trash in owncloud?


I'm using Python. I delete my files. They appear in trash.I have no idea how to delete them from trash.

import owncloud
oc = owncloud.Client('ip')
oc.login('account', 'password')
p=oc.list('/')
for i in p:
    oc.delete(i.path) # and than they appear in trash

Solution

  • There's currently no official API for the trashbin app in ownCloud, and therefore it's not integrated in ownCloud's python library. However there's a couple of hacky ways to achieve what you need. Once the files are in the trashbin, you can use the app's ajax API to:

    Remove all files from the trash

    If you don't care about the contents of the trashbin at all and just want to delete them:

    curl 'https://<owncloud-URL>/index.php/apps/files_trashbin/ajax/delete.php' \
        -H 'OCS-APIREQUEST: true' \
        -H 'Cookie: <session-token>' \
        --data 'allfiles=true&dir=/'
    

    List and selectively remove the files you want:

    You can request a list of the files in there:

    curl 'https://<owncloud-URL>/index.php/apps/files_trashbin/ajax/list.php?dir=/' \
         -H 'OCS-APIREQUEST: true' \
         -H 'Cookie: <session-token>'
    

    Notice that the dir query parameter on the url can be the same you are using to list-delete all your files in p=oc.list('/').

    Response body will look like this:

    {
      "data": {
        "permissions": 0,
        "directory": "\/",
        "files": [
          {
            "id": 0,
            "parentId": null,
            "mtime": 1505373301000,
            "name": "ownCloud Manual.pdf",
            "permissions": 1,
            "mimetype": "application\/octet-stream",
            "size": 5111899,
            "type": "file",
            "etag": 1505373301000,
            "extraData": ".\/ownCloud Manual.pdf"
          }
        ]
      },
      "status": "success"
    }
    

    Then you can create a list of the objects (files) you need to delete based on their names and mtimes:

    curl 'https://<owncloud-URL>/index.php/apps/files_trashbin/ajax/delete.php' \
        -H 'OCS-APIREQUEST: true' \
        -H 'Cookie: <session-token>' \
        --data 'files=["<file1>.d<mtime1>","<file2>.d<mtime2>"]&dir=/'
    

    Last note: <mtimeX> in the request is the property "mtime": 1505373301000 of the file when requesting the list and removing the 3 trailing zeroes. Also be aware of constructing the name by joining the 2 parts with the .d.

    Hope this helps!