Delightfully Simple – How to delete files in a document library after x hours, days or months

This question came up on Experts-Exchange.com today and after a few minutes of consideration, here’s my suggestion to the problem – simple, effective and customizable … and better yet, no code! More importantly, I hope this helps inspire ideas and creativity, by demonstrating how simple this really was, to implement.

 

The Business Requirement

 

We have a 3rd party application which sends daily reports (PDF’s) to a document library every day. Is there any other way we can delete reports older than a week automatically?

 

Assumptions

 

I am making one assumption here and that is that this document library is *dedicated* to storing just these PDF reports. If this library was used to store all sorts of documents, for other purposes as well (perhaps the PDF’s were stored in a single folder, within the document library?), we would need to add some additional logic such as checking the type of file, etc. and exiting out of the workflow if it wasn’t a ‘daily report’ PDF.

 

The Solution

 

A simple SharePoint Workflow, created in SharePoint Designer (remember, SPD is free now – get it at http://www.microsoft.com/spd – of course, proceeding with caution). Here are the steps to solve this particular requirement

 

  1. Create a SharePoint Designer Workflow that fires against newly created/edited items in the document library
  • Again, if this library is storing other types of documents as well, you will want to abort the workflow upon detecting that type of document
  1. Use the Pause Action to ‘wait’ until a week passes
  2. That’s it

 

If you wanted to, particularly for testing in the early going, you might want to write out history and/or email yourself to notify that the file has been deleted, to ensure it is working as expected. You’ll want to remove that action however, once you get the workflow going the way you want it.

 

The Visual Overview of the Solution

 

First off, we need a document library. Oh, good, here’s one!

 

 

Now we need to connect to our SharePoint site, using SharePoint Designer. Get SPD at http://www.microsoft.com/spd. Once it is installed, connect to your SharePoint site by choosing File, Open Site and plunking in the URL to the site (not to the library), such as http://portal.somewhere.com

 

 

 

 

Next, we need to create a new SharePoint Workflow. Note, you may get prompted to download and install the Windows Workflow Foundation before you can proceed. If you get that, er, do it, eh?

 

Choose File, New… Workflow…

 

 

Give the workflow a meaningful name, choose the correct library to fire against and choose the option to run against newly created items

 

 

Click Next and then create the first step, to pause for 7 days (using the Pause for Duration Action) and give this first step a meaningful name

 

 

Note: if the Pause for Duration Action isn’t in the dropdown list, simply choose the More Actions button (it likely won’t be, if you haven’t created any previous workflows)

 

 

 

Now, set the Pause for Duration to be the length of time you wish to wait, to delete the item. I’d suggest when you initially test the item, to set this to 5 minutes, so you don’t have to wait too long to test your results 🙂 … see below for how to modify an existing workflow.

 

Add a 2nd workflow step, to handle the ‘Delete’ function (which would run, once the Pause for Duration time period has passed)

 

 

Give the 2nd step a descriptive name, and an action of ‘Delete Item’. Then select the hyperlink that says ‘This List’ and choose the ‘Current Item’.

 

 

The 2nd step should look like the following, when complete.

 

 

The Workflow will validate and we’ll be in business.

 

 

Testing the Workflow

 

As mentioned, you may want to set your ‘Pause for Duration’ to be 5 minutes, instead of 7 days, until you are sure this baby is ready to roll. To test the workflow, add a new document to the library in whatever method you require (upload, WebDav/Publish, Email to Library, etc.).

 

You should notice that…

 

  • The document is gone after the 5 minutes has elapsed (ok, I’m really impatient and went for 2 minutes)

 

 

 

  • And in case you were curious… yes, it still sticks it in the Recycle Bin, where it should.

 

 

How to Modify an Existing Workflow

 

Finally, if you wanted to modify the workflow after the fact (alter the timeframe, say from the 5 minute test, to the 7 day requirement, it is easy to find and reopen/edit your workflow. Simply double click the .XOML file, under Workflows and then the name of your workflow (in our case, it was ‘Delete Daily Reports Older than 1 Week).

 

 

One final note. Don’t you hate when you try to write something original but then realize someone else had written the same damn thing, before you (honestly, this wasn’t lifted… I’m the first to even bring it up too 🙂

 

Anyways, props to these folks, who solved the same problem, the same way…  a long time before I did… http://www.sharepointblogs.com/holliday/archive/2007/07/26/delete-files-using-workflow-using-duration.aspx

 

 

8 responses to “Delightfully Simple – How to delete files in a document library after x hours, days or months

  1. Nice, thanks a lot – obvious when you think about it – but great job as you’ve done the thinking.

  2. When I add a new document by clicking “New” the work flow starts and the deletes the document in the specified time. My document library is email enabled. The workflow fails to start if the document library receives the document by email. Do you know of a solution?

      1. I had this same issue.. Did some more digging and found the resolution.. figured it may help others:

        SharePoint 2010 workflows triggered by an item added are fundamental to many business processes such as approval and document routing. Unfortunately if a list item is added via incoming email in a default configuration this new item will not trigger an item added workflow. This can limit the usefulness of the workflow. Fortunately this setting is easily changed via PowerShell.

        To determine what the current setting is for a farm run the following PowerShell commands in an elevated window (each command is one line followed by a return):

        $spWebService= [Microsoft.SharePoint.Administration.SPWebService]::ContentService

        $spWebService.DeclarativeWorkflowAutoStartOnEmailEnabled

        If the Value of the property is “False” you can enable it by typing the following commands (again each line and then return):

        $spWebService = [Microsoft.SharePoint.Administration.SPWebService]::ContentService

        $spWebService.DeclarativeWorkflowAutoStartOnEmailEnabled = $true

        $spWebService.Update()

        Thats it! Items added to lists via incoming email will now trigger item added workflows on the list. For the MSDN documentation of the property follow this link.

        Credit: http://spr.com/sharepoint-2010-workflows-from-email/

  3. Thought of the same scenario and implemented it SP2013 — My concern is the “Workflows in Progress” may not close the process properly. Case in point is when I restore the deleted file/folder, I still see “In Progress” status for the item.

    Is there a way to make sure that Workflow is completed/terminated after the Delete Item operation?

    1. Hey Fred, at first whiff, you could probably come up with some sort of text box/flagging concept for determining if the file is new, or new/restored. Probably based on the creation date somehow. I’m running out the door so I don’t have an immediate suggestion for this but that might well be an option.

      I suppose you could also just come up with a process for “Restored files” and make sure that after restoring, you change a flag to stop the workflow from initiating again.

      1. Thanks Sean.

        I’ve confirmed that the workflow completes the operation by adding a Stop workflow action.

        What previously threw me off was when the app ran another instance because “Creating a new item will start the workflow” was enabled in Start options.

        It made sense since the workflow needs to run again if the file gets restored from the Recycle bin.

Comments are closed.