How do I clean up files uploaded by "File Chooser"?

Theme

If you set "File Type to Select" to "Single Client File" in the "File Chooser", you can move files from Client-PC to PLP-Server.
This is a very useful feature. If you do not use this feature, you will be bothered by browser function errors (CORS ERR).

Subject

The problem here is that when you select a file in the browser, the file has already been transferred to the PLP-Server.
Therefore, clearing a selected file using the red X-icon on the Web browser will not delete the file from the PLP server.

Question

I am trying to solve this problem from the direction of JavaScript beforeunload-event, unload-event and Observer (I have attached the protocol I created).
But it's complicated and I'm stumped :-(
In the attached protocol, the path_observer holds the path to the uploaded files, and the protocol function (cleanFiles) to delete the uploaded files is executed beforeunload-event.
I hope there is an easier approach.

Does anyone know of a good approach to solving this issue?