How to cancel all pending uploads at once

Hello! I wanted to ask if there was a better way to cancel all pending uploads in a live upload form. I know there is cancel_upload but not sure if the best way would be something like reduce over all the uploads and use their refs with this function?

Any suggestions would be appreciated.

1 Like

Here’s how I do it. Allowed upload is named :images.

def handle_event("cancel-uploads", _params, socket) do
  entries = socket.assigns.uploads.images.entries

  {:noreply, Enum.reduce(entries, socket, &cancel_upload(&2, :images, &1.ref))}
end

Welcome to the forums!

2 Likes

Thank you! I ended up using this which is similar:

def handle_event("clear", _params, socket) do
  entries = socket.assigns.uploads.images.entries

  updated_socket = 
    Enum.reduce(entries, socket, fn entry, acc ->
      cancel_upload(acc, :images, entry.ref)
    end)

  {:noreply, updated_socket}
end
1 Like

Ha, yep, exactly the same thing just using fn instead of the capture syntax. I should probably not use the capture syntax as I usually don’t if there are more than one argument.

1 Like