locked
Empty huge Recycle bin of SharePoint 2007 site RRS feed

  • Question

  • Hi,

    I am unable to empty sharepoint 2007 site recycle bin. Everytime I tried to recycle it, I got following error message

    Cannot insert the value NULL into column 'DiskUsed', table 'WSS_Content_2653.dbo.SiteQuota'; column does not allow nulls. INSERT fails.
    Cannot insert the value NULL into column 'DiskUsed', table 'WSS_Content_2653.dbo.SiteQuota'; column does not allow nulls. INSERT fails.
    The statement has been terminated. 
     ...

    Any clues how to resolve this issue ?


    Thanks


    Muhammad Tariq khan
    Wednesday, January 4, 2012 9:12 AM

Answers

  • HI,

    First try to delete all through the code as shown:

    using (SPSite site = new SPSite("YourSiteUrl"))
                {
                using(SPWeb web = site.OpenWeb())
                {
                    SPRecycleBinItemCollection rItems = web.RecycleBin;
                    rItems.DeleteAll();
    }
    }
    


    If it does not help delete each item one by one using the code below

    using (SPSite site = new SPSite("YourSiteUrl"))
                {
                using(SPWeb web = site.OpenWeb())
                {
                    SPRecycleBinItemCollection rItems = web.RecycleBin;
                   
                    foreach (SPRecycleBinItem rItem in rItems)
                    {
                        rItem.Delete();
                        
                    }
    
    
               
              
            }
            }
    
    I hope this will help you out.


    Thanks, Rahul Rashu
    Thursday, January 5, 2012 4:33 AM
  • Hi,

    The code I have provided is for the web (subsite) level recycle bin.

    Are you trying to empty site collection recycle bin?

    If yes you need to do a small change in code

    SPRecycleBinItemCollection rItems = site.RecycleBin;
    



    Thanks, Rahul Rashu
    • Marked as answer by Mike Walsh FIN Wednesday, January 18, 2012 9:46 AM
    Tuesday, January 17, 2012 4:11 AM
  • Dear Rashu,

    Really appreciate your efforts for solving my problem...

    FYI - Following is the source code which I successfully used in my environment...

       #region SharePoint Delete RecycleBin Items
                using (SPSite mySite = new SPSite("http://YourSiteURL"))
                {
                    bool doLoop = true;
                    while (doLoop)
                    {
                        try
                        {
                            //Empty the items from the SiteRecycleBin (the second stage recycle bin)   
                            if (mySite.RecycleBin.BinType == SPRecycleBinType.SiteRecycleBin)
                            {
                                int startCount = mySite.RecycleBin.Count;

                                if (startCount < 1)
                                    doLoop = false;

                                //See the number of items before the delete
                                Console.Write("There are currently: " + startCount + " items in the Recycle Bin.\n");

                                //Delete all the items
                                mySite.RecycleBin.DeleteAll();


                                //See the number of items after the delete
                                Console.Write("\nThere are now: " + startCount + " items in the Recycle Bin, after deletion.\n");
                            }

                            //Make sure we dispose properly
                            if (mySite != null)
                            {
                                mySite.Dispose();
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }
                    }
                }
                #endregion
                Console.WriteLine("Recycle bin emptied");
                Console.WriteLine("Press any key to exit");
                Console.ReadLine();


    Muhammad Tariq khan
    Wednesday, January 18, 2012 9:38 AM

All replies

  • Are you deleting the items from SiteCollection recycle bin or from site RecycleBin

    Try clearing the SiteCollection recycle bin... looks like you have set some quota which is because of that you are not able to delete the items..

    Or

    Trying deleting from code

    Web.RecycleBin.DeleteAll()


    Manoj Gangwar | Sharepoint 2010 MCPD | Sharepoint 2010 MCTS
    Wednesday, January 4, 2012 12:28 PM
  • remove the quota from the site collection and try again.
    MCITP|MCTS SharePoint
    Wednesday, January 4, 2012 3:35 PM
  • Hi,

    Instead of emptying  try to delete one item at a time and check if you still get this error.

    If this also does not work out for you then go to the central admin and remove the quota for this site collection and then carry out this operation.

    Once you have completed this apply quota again.

    I hope this will help you out.


    Thanks, Rahul Rashu
    Wednesday, January 4, 2012 8:12 PM
  • Hello All,

    I am using site collection recycle bin to delete the items. As from there i can delete 200 items at a time. Also I have checked that in "Site Collection Quota an Locks", value of Individual Quota template is not setted (means infinite)....

    I can delete 1 item at a time from recycle bin ... But if I continue to delete with this frequency then it will take weeks to empty the recycle bin as there are thousands of items.

    What to do ?


    Muhammad Tariq khan

    Thursday, January 5, 2012 4:14 AM
  • HI,

    First try to delete all through the code as shown:

    using (SPSite site = new SPSite("YourSiteUrl"))
                {
                using(SPWeb web = site.OpenWeb())
                {
                    SPRecycleBinItemCollection rItems = web.RecycleBin;
                    rItems.DeleteAll();
    }
    }
    


    If it does not help delete each item one by one using the code below

    using (SPSite site = new SPSite("YourSiteUrl"))
                {
                using(SPWeb web = site.OpenWeb())
                {
                    SPRecycleBinItemCollection rItems = web.RecycleBin;
                   
                    foreach (SPRecycleBinItem rItem in rItems)
                    {
                        rItem.Delete();
                        
                    }
    
    
               
              
            }
            }
    
    I hope this will help you out.


    Thanks, Rahul Rashu
    Thursday, January 5, 2012 4:33 AM
  • Dear Rashu,

    I am still getting error. Following error message is appearing

    Transaction (Process ID 160) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) 
       at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) 
       at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) 
       at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) 
       at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) 
       at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) 
       at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) 
       at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) 
       at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() 
       at Microsoft.SharePoint.Utilities.SqlSession.ExecuteNonQuery(SqlCommand command) 
       at Microsoft.SharePoint.SPRecycleBinUtility.DeleteSingle(SPSite site, SPWeb web, Guid id) 
       at Microsoft.SharePoint.SPRecycleBinUtility.DeleteMultiple(SPSite site, SPWeb web, Guid[] ids, Boolean ignoreNotFound, Int32& returnValue, Guid& failedItem) 
       at Microsoft.SharePoint.SPRecycleBinUtility.Delete(SPSite site, SPWeb web, Guid[] ids, Boolean ignoreNotFound, Int32& returnValue, Guid& failedItem) 
       at Microsoft.SharePoint.SPRecycleBinItemCollection.DeleteCore(Guid[] ids, Boolean ignoreNotFound) 
       at Microsoft.SharePoint.SPRecycleBinItemCollection.Delete(Guid[] ids) 
       at Microsoft.SharePoint.ApplicationPages.RecycleBinPage.ProcessAction(String action, String guidString) 
       at Microsoft.SharePoint.ApplicationPages.RecycleBinPage.OnLoad(EventArgs e) 
       at System.Web.UI.Control.LoadRecursive() 
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)


    Muhammad Tariq khan
    Monday, January 9, 2012 5:53 AM
  • Is there any manual way to safely empty sharepoint recycle bin by using sql command on sharepoint content database or something like that ?
    Muhammad Tariq khan
    Monday, January 9, 2012 4:50 PM
  • Hi,

    Whether this error came straight a way when you executed the code?

    If it has come after executing a number of items then you can execute it again.

    If it has come on a specific item then delete than and execute it again.

    Do let me know these so that if none of these works I will modify the code to execute in batches rather than all.


    Thanks, Rahul Rashu
    Tuesday, January 10, 2012 4:37 AM
  • Try to rest your iis. It will clear if any resource are in use. Then run the code
    Thanks, Saravanan PRS
    Tuesday, January 10, 2012 5:35 AM
  • Dear Rashu,

    When I am running your code which removes recycle bin items one by one, following error message is appearing 

    collection was modified; enumeration operation may not execute

    Even I added Exception Handling code but no success....


    Muhammad Tariq khan
    Wednesday, January 11, 2012 7:40 AM
  • Hi,

    have you tried the first code snippet or the second one?

    It seems that you have tried the second one. I have modified it so that the above exception will not come:

    using (SPSite site = new SPSite("YourSiteUrl"))
                {
                    SPWeb web = site.OpenWeb();
                    SPRecycleBinItemCollection rItems=  web.RecycleBin;
                    SPRecycleBinItem[] rItemArray = new SPRecycleBinItem[rItems.Count];
                    int i = 0;
                    foreach (SPRecycleBinItem rItem in rItems)
                    {
                        rItemArray[i] = rItem;
                        i++;
                    }
                    for (int j = 0; j < rItemArray.Length; j++)
                    {
                        rItemArray[j].Delete();
                    }
                    
                    web.Update();
                    
                }
    
    <br/>
    
    <br/>
    


    Thanks, Rahul Rashu
    Wednesday, January 11, 2012 6:10 PM
  • Dear Rashu,

    Really appreciate your help and support...

    I have tried your new code on my test machine... Now previous error (collection was modified; enumeration operation may not execute) has successfully gone ... But if i tried to remove the items from End User Recycle Bin (http://intranet/_layouts/AdminRecycleBin.aspx) then nothing happens...  Also I have modified your code as follows to check which item is currenly under deletion process for better viewing...

    Please let me know If am doing any mistake ? The URL which I am typing in the code is correct should I modify it ???

    Code:

    using (SPSite site = new SPSite("http://intranet/_layouts/AdminRecycleBin.aspx"))
                {
                    SPWeb web = site.OpenWeb();
                    SPRecycleBinItemCollection rItems = web.RecycleBin;
                    SPRecycleBinItem[] rItemArray = new SPRecycleBinItem[rItems.Count];
                    Console.WriteLine(rItems.Count);
                    int i = 0;
                   
                    foreach (SPRecycleBinItem rItem in rItems)
                    {
                        Console.WriteLine("\nDeleted item: " + rItem.ID + " " +rItem.Title);
                        rItemArray[i] = rItem;
                        i++;
                    }
                    for (int j = 0; j < rItemArray.Length; j++)
                    {
                        Console.WriteLine("\n rItemArray" + j);
                        rItemArray[j].Delete();
                    }

                    web.Update();

                }
                Console.ReadLine();

     

    Thanks in advance


    Muhammad Tariq khan
    Thursday, January 12, 2012 11:22 AM
  • Hi,

    The code I have provided is for the web (subsite) level recycle bin.

    Are you trying to empty site collection recycle bin?

    If yes you need to do a small change in code

    SPRecycleBinItemCollection rItems = site.RecycleBin;
    



    Thanks, Rahul Rashu
    • Marked as answer by Mike Walsh FIN Wednesday, January 18, 2012 9:46 AM
    Tuesday, January 17, 2012 4:11 AM
  • Dear Rashu,

    Let me try again with your suggested changes... I will get back to you after verifying the same... 

     

    Thanks 


    Muhammad Tariq khan
    Tuesday, January 17, 2012 4:48 AM
  • Dear Rashu,

    Really appreciate your efforts for solving my problem...

    FYI - Following is the source code which I successfully used in my environment...

       #region SharePoint Delete RecycleBin Items
                using (SPSite mySite = new SPSite("http://YourSiteURL"))
                {
                    bool doLoop = true;
                    while (doLoop)
                    {
                        try
                        {
                            //Empty the items from the SiteRecycleBin (the second stage recycle bin)   
                            if (mySite.RecycleBin.BinType == SPRecycleBinType.SiteRecycleBin)
                            {
                                int startCount = mySite.RecycleBin.Count;

                                if (startCount < 1)
                                    doLoop = false;

                                //See the number of items before the delete
                                Console.Write("There are currently: " + startCount + " items in the Recycle Bin.\n");

                                //Delete all the items
                                mySite.RecycleBin.DeleteAll();


                                //See the number of items after the delete
                                Console.Write("\nThere are now: " + startCount + " items in the Recycle Bin, after deletion.\n");
                            }

                            //Make sure we dispose properly
                            if (mySite != null)
                            {
                                mySite.Dispose();
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }
                    }
                }
                #endregion
                Console.WriteLine("Recycle bin emptied");
                Console.WriteLine("Press any key to exit");
                Console.ReadLine();


    Muhammad Tariq khan
    Wednesday, January 18, 2012 9:38 AM