SPFileCollection.Delete method

Deletes the file located at the specified URL.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

Syntax

public void Delete(
    string urlOfFile
)

Parameters

Exceptions

Exception Condition
SPException

An error was encountered while performing the operation.

Remarks

This method deletes the file located at the URL specified by the urlOfFile parameter from the file collection.

Examples

The following code example iterates through all the files in the Shared Documents document library of every subsite for a site and deletes files that were not modified within the last week.

using (SPSite oSiteCollection = new SPSite("https://localhost"))
{
    SPWebCollection collWebsites = oSiteCollection.AllWebs["MyWebSite"].Webs;

    DateTime dtCutoffDate = DateTime.UtcNow.AddDays(-7);

    foreach (SPWeb oWebsite in collWebsites)
    {
        SPFileCollection collFiles = oWebsite.GetFolder("Shared Documents").Files;

        for (int intIndex=collFiles.Count - 1; intIndex>-1; intIndex--)
        {
            if (collFiles[i].TimeLastModified < dtCutoffDate)
            {
                string strDelUrl = collFiles[intIndex].Url;
                try
                {
                    collFiles.Delete(strDelUrl);
                }
                catch {}
            }
        }

        oWebsite.Dispose();
    }
}

Note

Certain objects implement the IDisposable interface, and you must avoid retaining these objects in memory after they are no longer needed. For information about good coding practices, see Disposing Objects.

See also

Reference

SPFileCollection class

SPFileCollection members

Microsoft.SharePoint namespace