How to delete file in trash in owncloud?

1.7k Views Asked by At

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
2

There are 2 best solutions below

0
On BEST ANSWER

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!

0
On

Thanks for kuchiya! I delete files from trash.

import requests

userID = 'aaa'
userPassword = 'bbbbbbb'

with requests.Session() as s:
    response = s.get('http://'+userID+':'+userPassword+'@<owncloud-URL>/index.php/apps/files/?dir=/&fileid=4896' ) #change your fileid 
    token = response.content.split('data-requesttoken="')[1].split('"')[0]

    Cookie = 'oc7b6t1tjo61='+s.cookies['oc7b6t1tjo61']+';oc_sessionPassphrase='+s.cookies['oc_sessionPassphrase']

    data = {'allfiles':'true', 'dir':'/'}
    headers = {'requesttoken':token, 'OCS-APIREQUEST':'true', 'Cookie':Cookie ,'Accept-Language':'zh-TW,en-US;q=0.7,en;q=0.3'}
    response2 = s.post('http://<owncloud-URL>/index.php/apps/files_trashbin/ajax/delete.php',data = data, headers = headers, cookies=s.cookies)
    print response2.content