web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / How to prevent attachm...
Power Apps
Suggested Answer

How to prevent attachment file duplicates

(0) ShareShare
ReportReport
Posted on by

Hi all,

 

I’m trying to prevent users from submitting duplicate files in the attachment section.

I added the following logic in the OnAddFile property. It successfully detects duplicate files, but it does not remove the duplicate from the attachment control.

I would prefer not to use Reset(Self), since that would remove all files. Instead, I’d like to:

- remove only the duplicate file, or

- ensure that if the same file is uploaded multiple times, it only appears once in the attachments list.


     
With(
{ newName: Lower(Last(Self.Attachments).Name) },
    If(
        CountIf(Self.Attachments As f, Lower(f.Name) = newName) > 1,
        Notify("This file has already been uploaded.")
    )
)
iShot2026-03-20 14.25.45.png
I have the same question (0)
  • Suggested answer
    WarrenBelz Profile Picture
    155,352 Most Valuable Professional on at
    This should work for you - put this OnAddFile of the Attachment Control
    If(
       CountIf(
          colAttach, 
          Lower(Name) = Lower(Last(Self.Attachments).Name)
       ) > 0,
       Notify(
          "This file has already been uploaded",
          NotificationType.Error,
          5000
       );
       Reset(Self),
       Collect(
          colAttach, 
          Last(Self.Attachments)
       )
    )
    and set the Items of the Attachment control to 
    colAttach
     
    The problem is that you cannot programatically delete an item from an attachment control, so you nned to use a collection for the Items and manage that. Resetting the control in this instance will "delete" the new item and revert it back to the existing collection without it added.
     
    Please Does this answer your question if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider answering Yes to Was this reply helpful? or give it a Like
    Visit my blog
    Practical Power Apps    LinkedIn  
  • Suggested answer
    11manish Profile Picture
    2,293 on at
    You’re very close  — the challenge here is a limitation of the Attachment control in Microsoft Power Apps.
     
    Key Limitation (Important)
    • The Attachment control is not fully controllable
    You cannot remove a single file programmatically
    • Reset(Self) → removes ALL files only
    • No API to delete just one attachment
    So your current approach detects duplicates , but cannot remove them 
     
    Best Solution (Recommended Pattern)
     
    Control uploads using a temporary collection
    Instead of relying directly on Self.Attachments, you:
    • Intercept → Validate → Store only unique files
    Step-by-step solution
    1. Create a collection
    • ClearCollect(colAttachments, [])
    2. Use OnAddFile logic
    Replace your code with:
     
    With(
        { newFile: Last(Self.Attachments) },
        If(
            CountIf(colAttachments, Lower(Name) = Lower(newFile.Name)) > 0,
            Notify("This file has already been uploaded."),
            
            Collect(colAttachments, newFile)
        )
    )

    3. Reset control (safe now)
     
    Reset(Self)
     
    Why this works:
    • You store only valid files
    • Reset clears UI
    • Then you re-bind from collection
    4. Bind attachment control
    Set:
     
    Items = colAttachments

    Thanks
    Manish
     
     
  • WarrenBelz Profile Picture
    155,352 Most Valuable Professional on at
    That is exactly what I posted . . .
  • WarrenBelz Profile Picture
    155,352 Most Valuable Professional on at
    A quick follow-up to see if you received the answer you were looking for. Happy to assist further if not.
     
    Please Does this answer your question if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider answering Yes to Was this reply helpful? or give it a Like ♥
    Visit my blog
    Practical Power Apps    LinkedIn   
  • Suggested answer
    CU02041602-0 Profile Picture
    2 on at
    This is very helpful but I have a query regarding uploading multiple files at once . If I have to attach more than two files at a time, so it will work fine or it will get issue.
  • WarrenBelz Profile Picture
    155,352 Most Valuable Professional on at
    This is a very tricky one - I spent some time on a model, and this seems to work. First when you open the Form, you need to initialise a couple of Collections with the records you are working on - you might do this when you navigate to the record,
    ClearCollect(
       colFiles,
       LookUp(
          YourSPList,
          ID = YourID
       ).Attachments
    );
    ClearCollect(
       colTemp1,
       ForAll(
          colFiles As _Files,
          {
             Value: _Files.Value,
             Name: _Files.DisplayName,
             DisplayName: _Files.DisplayName,
             Id: _Files.Id,
             AbsoluteUri: _Files.AbsoluteUri
          }
       )
    );
    This gets the existing attachments - the structure accounts for files already stored as there are additional values not present if you just collect the Attachment Control contents.
     
    The Items of your Attachment Control
    If(
       IsEmpty(colFiles),
       ThisItem.Attachments,
       colFiles
    )
    Now put this OnAddFile of the Attachment Control
    Clear(colTemp2);
    Collect(
       colTemp1,
       Self.Attachments
    );
    With(
       {
          _Names: Distinct(
             colTemp1,
             Lower(DisplayName)
          )
       },
       Collect(
          colTemp2,
          ForAll(
             _Names As _N,
             LookUp(
                colTemp1,
                Lower(DisplayName) = _N.Value
             )
          )
       );
       ClearCollect(
          colFiles,
          colTemp2
       );
       Reset(Self);
       If(
          CountRows(_Names) <> CountRows(colTemp1),
          Notify(
             "Duplicates removed",
             NotificationType.Information,
             5000
          )
       )
    );
    
    Esentially, you are collecting  the entire contents of the attachment control and adding it to whatever was there before (so you will have duplicates of both existing items and anything duplicate the user has added, then you find a unique set of it all to create the Collection forming the Items of the Attachment control. A couple of counts are done then done to see if the number of attachments has been reduced by applyng the Distinct filter.
     
    and OnRemoveFile
    RemoveIf(
       colFiles,
       DisplayName = ThisItem.DisplayName
    )
    There still may be some glitches - but best effort considering the system was never designed to do this.
     
    Please Does this answer your question if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider answering Yes to Was this reply helpful? or give it a Like
    Visit my blog
    Practical Power Apps    LinkedIn  
     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Users!

Kudos to our 2025 Community Spotlight Honorees

Congratulations to our 2025 community superstars!

Congratulations to the April Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
Vish WR Profile Picture

Vish WR 875

#2
Valantis Profile Picture

Valantis 530

#3
11manish Profile Picture

11manish 432

Last 30 days Overall leaderboard