> ## Content Index
> Fetch the complete content index at: https://blog.tsd.digital/llms.txt
> Use this file to discover other available public pages before exploring further.

# Delete all UCommerce baskets older than x days
- URL: https://blog.tsd.digital/delete-all-ucommerce-baskets-older-than-x-days/
- Published: 2010-10-04T08:14:31.000Z
- Updated: 2010-10-04T08:14:31.000Z
- Author: Tim Gaunt
- Tags: Development, e-commerce, SQL, SQL Server, Ucommerce, Umbraco, #Import 2025-04-01 05:37

After my last [UCommerce](http://www.ucommerce.dk/?ref=blog.tsd.digital) post on [how to delete test orders and baskets from UCommerce](http://blogs.thesitedoctor.co.uk/tim/2010/10/01/Deleting+Test+Orders+And+Baskets+From+UCommerce.aspx?ref=blog.tsd.digital), Søren suggested I extended the delete all baskets code to take into account when it was created. As my last code was relating to deleting test orders/baskets (and so would want to get rid of them all), I decided to post this one separately.

## Delete all baskets older than x days

To use this, all you need to do is change the @addedBefore parameter to whatever date/time you want (or just adjust the –7 which represents seven days in the past.

\--Delete all carts purchaseorders and associated data within x days DECLARE @addedBefore smalldatetime --By default the script deletes everything older than 7 days SET @addedBefore = DATEADD(dd, -7, GETDATE()) BEGIN TRAN UPDATE uCommerce\_PurchaseOrder SET BillingAddressId = NULL WHERE OrderNumber IS NULL AND CreatedDate <= @addedBefore DELETE a FROM uCommerce\_Shipment a INNER JOIN uCommerce\_PurchaseOrder b ON a.OrderId = b.OrderId WHERE b.OrderNumber IS NULL AND b.CreatedDate <= @addedBefore DELETE a FROM uCommerce\_OrderAddress a INNER JOIN uCommerce\_PurchaseOrder b ON a.OrderId = b.OrderId WHERE b.OrderNumber IS NULL AND b.CreatedDate <= @addedBefore DELETE a FROM uCommerce\_OrderProperty a INNER JOIN uCommerce\_PurchaseOrder b ON a.OrderId = b.OrderId WHERE b.OrderNumber IS NULL AND b.CreatedDate <= @addedBefore DELETE a FROM uCommerce\_OrderLine a INNER JOIN uCommerce\_PurchaseOrder b ON a.OrderId = b.OrderId WHERE b.OrderNumber IS NULL AND b.CreatedDate <= @addedBefore DELETE a FROM uCommerce\_Payment a INNER JOIN uCommerce\_PurchaseOrder b ON a.OrderId = b.OrderId WHERE b.OrderNumber IS NULL AND b.CreatedDate <= @addedBefore DELETE a FROM uCommerce\_OrderStatusAudit a INNER JOIN uCommerce\_PurchaseOrder b ON a.OrderId = b.OrderId WHERE b.OrderNumber IS NULL AND b.CreatedDate <= @addedBefore DELETE FROM uCommerce\_PurchaseOrder WHERE OrderNumber IS NULL AND CreatedDate <= @addedBefore --Uncomment this --COMMIT TRAN --And comment out this ROLLBACK TRAN