Programmatically deleting list items/documents with Recycle Bin support

, , ,

If you delete a document in SharePoint 2007 using the User Interface, it’s being moved to the Recycle Bin, so that it can be restored if necessary. There are situations when you want to include deleting list items and documents in your custom solutions. The most commonly used approach I’ve seen is calling the SPListItem.Delete() method. While this does the job, and deletes the item, it deletes it permanently instead of moving to the Recycle Bin.

Looking carefully at the SPListItem class, you can find the not-that-popular SPListItem.Recycle() method. Its description (Recycles the item and returns the item's GUID.) is rather cryptic and doesn’t give you much clue on what you could use is for. It turns out that it’s exactly that method that you need to call in order to move a list item/document to the Recycle Bin instead of deleting it permanently.

In general moving items to the Recycle Bin instead deleting them permanently is what you should do in your custom solutions. It is standard SharePoint behavior and therefore something end users will expect of your solutions as well. You should perform the permanent deletion only if explicitly named in the requirements. Otherwise, let’s just stick to what SharePoint does to leverage the same User Experience.

Under the hood

Internally there isn’t much difference between the SPListItem.Delete and SPListItem.Recycle methods. Both call an internal SPListItem.Delete method with a different parameter which determines whether an item should be moved to the Recycle Bin or permanently deleted.


Possibly related posts

10 Responses to “Programmatically deleting list items/documents with Recycle Bin support”

  1. Marc Charmois Says:

    Thank you very much Waldek, it is exactly what I was looking for.

    Marc

  2. Waldek Mastykarz Says:

    @Marc Charmois: glad to help :)

  3. Hemu Says:

    Thanks.Perfect one.

  4. vasudha Says:

    Hi,
    Just wanted to know if the same will be applicable while deleting the versions in the document library.

    I am using DocItem.File.Versions[i].Delete();

    Will this send the fileversion to the recycle bin or will it directly delete the version from the site?

  5. Waldek Mastykarz Says:

    @vasudha: DocItem.File.Versions[i].Delete(); deletes the file permanently. It's hardcoded so you cannot change the default behavior.

  6. vasudha Says:

    Thanks for the reply Waldek.
    My requirement is just that.
    I want to delete the versions permanently and not to send them to recycle bin.
    Just wanted to confirm on that one.
    Thanks again.

  7. Dan Meineck Says:

    Hi Waldek, thanks for the useful tip. How do you achieve the same thing using the Lists webservice UpdateListItems method? Passing in the CAML statement is simply the 'Delete' Cmd attribute value – how do you recycle via the UpdateListItems method?

  8. Waldek Mastykarz Says:

    @Dan Meineck: I haven't tried it with the WebServices yet, so it's definitely an interesting question whether it can be done at all. I will post here an answer if I find something more about it.

  9. Raj Says:

    gud post

  10. khushi Says:

    Hi Waldek,

    http://www.devexpertise.com/2009/02/04/deleting-list-items-in-a-sharepoint-list/

    article says that if you use SPItem.Delete() gives error.

    Collection was modified; enumeration operation may not execute.

    So, Only the correct method is via SPitem.Delete[i].

    Could you please check as the above mentioned article conflicts with yours?

    Thanks
    Khushi.

Leave a Reply

Security Code:

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS
Copyright © 2007 - 2012 Waldek Mastykarz

Creative Commons License