Function with Programming Rigths?

Hello,
I try to delete every attachment with velocity and i found this code :

$mydoc.document.deleteAttachment($mydoc.getAttachment(“Doc1.doc”).attachment, $context.context)
$mydoc.save()

When i run the code snippet i have code lines displaying without remove file…

Do you have an idea ?

Thanks,
Arnaud

PS : I try $doc and $doc.getDocument() in the same place of $mydoc

You’ve used the {{velocity}} macro right?

I had a lot of other lines functionnal
Now I test with only these lines

{{velocity}}
#foreach($attachment in $doc.getAttachmentList())
$mydoc.deleteAttachment($attachment,$context.context)
$mydoc.getDocument().save()
#end
{{/velocity}}

And it displays

$mydoc.deleteAttachment($attachment,$context.context)
$mydoc.getDocument().save()

for the number of attachments

Is it really $mydoc I have to used ?

If you want to reference the current doc, then you should replace $mydoc with $doc.

I have replaced $mydoc by $doc and it always displays lines without interpreting

it just means that the API you’re calling doesn’t exist. Make sure to check the API signature.

Just checked quickly and I don’t see any deleteAttachment API in Document or XWikiDocument :wink:

I see there’s a removeAttachment but in XWikiDocument, not in Document

I do not see removeAttachment in SRD for XWikiDocument
deleteAttachment is in XWikiDocument
Furthermore, I put Attachment instead of XWikiAttachment

The right code is

{{velocity}}
#foreach($attachment in $doc.getAttachmentList())
$doc.getDocument().deleteAttachment($attachment.getAttachment(), $context.context)
#end
{{/velocity}}

Ah deleteAttachment is a legacy method (which is why I missed it). You should prefer removeAttachment instead.

Note that your example requires PR but I don’t think there’s another way FTM.

Thanks :grinning:
I will test at the customer in the next days !
Could you say me arguments of removeAttachment ?
I don’t see the function in the documentation…
And what is PR ?

See xwiki-platform/xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/doc/XWikiDocument.java at 56371b2dc4c4941c795a28b49cf03fcbc9bfbda8 · xwiki/xwiki-platform · GitHub and below

Programming Rights

I have tested at the customer friday

$context.context and $doc.getDocument() don’t function (just display)

Could you confirm that they require Programming rights ?
I will see int the next days with the computing services of the customer if they can give me Programming rights