How to Replace an Existing SharePoint Folder with the CSOM API
Question details
The user needs to overwrite or replace an existing destination folder in SharePoint using the CSOM API, similar to the overwrite functionality found in Power Automate.

- Product
- SharePoint
- Device & OS
- not provided
- Scenario
- Copying or moving folders programmatically in SharePoint where the destination folder already exists.
- Observed behavior
- The CSOM MoveCopyUtil.CopyFolderByPath method only provides the KeepBoth option via MoveCopyOptions and lacks a direct parameter to overwrite an existing folder.
Ensure you have the latest SharePoint Client-Side Object Model (CSOM) SDK installed and that your API credentials have the necessary permissions to read, write, and delete folders in the target site.
Use a CSOM Delete and Re-Copy Workaround
Since MoveCopyOptions lacks an overwrite feature, manually check for the folder's existence, delete it if present, and then perform the copy operation.
The CSOM MoveCopyUtil.CopyFolderByPath method natively restricts behavior to KeepBoth. To mimic an overwrite action using purely CSOM, developers must implement a two-step programmatic approach to clear the destination before copying.
Use the Web.GetFolderByServerRelativeUrl method to check if the destination folder already exists in the target SharePoint library.
If the folder exists, call the .DeleteObject() method on the folder object and invoke ExecuteQuery() to remove the existing destination folder.
Once the destination path is clear, execute MoveCopyUtil.CopyFolderByPath to copy the source folder to the now-empty destination.
Evaluate the SharePoint REST API
Switch from CSOM to the SharePoint REST API, which offers more granular parameters for handling file and folder replacements.
Manage Your SharePoint Documents Seamlessly with WPS Office
While managing SharePoint folder structures programmatically requires developer APIs, editing the actual documents within those folders is effortless with WPS Office. As a lightweight, feature-rich office suite, it provides everything you need to view, create, and edit your professional documents without heavy subscription fees.

Frequently Asked Questions
Does MoveCopyOptions in CSOM support overwriting folders?
No. Currently, the SharePoint CSOM MoveCopyOptions class only exposes the KeepBoth option, meaning it will retain both folders rather than replacing the destination.
Why does Power Automate allow folder overwriting while CSOM does not?
The SharePoint connector in Power Automate uses a different backend abstraction that explicitly handles the deletion or replacement logic for you within its 'Copy folder' action, bypassing the native CSOM limitation.
Where can I find official help for SharePoint REST API endpoints?
For detailed developer guidance, implementation syntax, and API limitations, you should visit the Microsoft Q&A community forums (learn.microsoft.com/en-us/answers) and tag your question with SharePoint Development.




