Delete all documents which have no parent when opening new form

I hope you can help me with this. I have included a library in my form.
However, if a document is uploaded and the form is not saved, the document is displayed again the next time the form is opened.
Is there a way to delete all existing documents without parent from this user the next time the form is opened?
I currently have a view that only shows all documents of a user.

Best regards

Hello @Sternchen,

You can delete document from the Document Library using PnPjs function:


You can delete all files that are displayed in List or Library control like so:

var docuemnts = fd.control('SPDataTable1');

docuemnts.forEach(doc => 

How can I intercept beforehand that it only executes when documents are there?
Currently I get the error message from undefined. For example, with users who open the form for the very first time. No documents can be displayed for them.
Even if there is a document there, this error message comes up.