Delete a Folder from a Sharepoint Document Library


This is an update to a post I wrote on deleting list items from document libraries, prompted by a comment made on that post asking how to delete Folder(s) from within a document library.

Initially I replied by suggesting the use of the SharePoint object mode, as shown below;

SPWeb web = new SPSite("your site's url").OpenWeb();
web.Folders[“document library name”].SubFolders.Delete(“folder name”);

However, the commenter indicated that he could not use the object model as the code would be executed on a remote client machine, which left the SharePoint Webservices.

I’d not previously attempted to delete folders using CAML before, and as it turns out the CAML is exactly the same as if we were deleting list items, see the post on deleting items from document libraries, but the CAML for deleting a folder named Folder2 in some document library is shown below;

<Batch PreCalc='TRUE' OnError='Continue'>
    <Method ID='1' Cmd='Delete'>
        <Field Name='ID'>2</Field>
        <Field Name='FileRef'>http://pccal01/mortality/Test/Folder2</Field>
    </Method>
</Batch>

I’ve shown the CAML above with spaces and such for display purposes, your CAML should not contain white space or CR characters. In the second example shown below, the folder name contains a space, "Folder 1", in this case the FileRef parameter should not be URL encoded, or contain _x0020_ replacements etc.

<Batch PreCalc='TRUE' OnError='Continue'>
    <Method ID='1' Cmd='Delete'>
        <Field Name='ID'>1</Field>
        <Field Name='FileRef'>http://pccal01/mortality/Test/Folder 1</Field>
    </Method>
</Batch>

Note: The ID parameter is required to be present in the CAML query, but it is ignored by the webservice, you can safely just set its value to “1”.

These CAML commands can be submitted to SharePoint using the UpdateListItems method of the Lists webservice found at  http://{sharepoint site URL}/_vti_bin/Lists.asmx

Published by

Phil Harding

SharePoint Consultant, Developer, Father, Husband and Climber.

7 thoughts on “Delete a Folder from a Sharepoint Document Library

  1. 3
    1

    http://Server/%5Bsites/%5D%5BSite/%5DShared
    Documents/Folder
    1
    Name

    We’re using sahrepoint web service and CAML above to update folder(mainly rename) but it only works for 1 time. The 2nd time the UpdateListItems is called, we’ll get a Save Conflict error saying another use is changing….

    Do you know how to resolve this issue? Thanks!

  2. it seems the caml is not well shown.

    the detailed error we got are:

    Save Conflict

    Your changes conflict with those made concurrently by another user. If you want your changes to be applied, click Back in your Web browser, refresh the page, and resubmit your changes.

  3. Jackie, without seeing your CAML, I can’t really help. I’m assuming you’re not sending the same batch of CAML more than once, you said you were trying to rename folders?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.