Hi,
Trevor's script is correct; I would recommend to use API with our provider methods for all operations... however, in this case, it still isn't enough. To put it more simply, the visitor's shopping cart object is primarily saved as a session variable, which means that there's no direct access to it from different request (which the scheduled task typically runs within). Even if you remove the items from DB or using API, the controls or web parts that display the cart content still use the data from session of the visitor - customer.
If you remove an item from the cart outside the session, any change that you make to the shopping cart object will be saved to the database and the application cache, however it will not be visible to the customer until their shopping cart is reloaded. This is because all of the live site web parts or controls will still keep the previous instance of the shopping cart in the session variable.
Unfortunately there is no example of clearing the shopping cart active in different session available. This scenario is rather uncommon, so it is not yet covered by our documentation or knowledge base. Currently (and in version 7), the only way to achieve the scenario is to either ensure your code uses the visitor's session, or develop a clearing mechanism that will detect your changes and reload the EcommerceContext.CurrentShoppingCart property.
One possible way (actually used by one of our customers) could be creating a custom table and write cart IDs into there from the scheduled task. Then you could check those values in the web parts (version 8+) or controls (which display cart content) if the current cart id exists in the table, and optionally empty the session variable.
I would just add that in version 7 you can detect the changes in visitor's context running code e.g. in the ShoppingCart.ascx control or similarly in relevant places (where cart content or data are displayed, or loaded).
In later 8.2 version and in 9, it should be possible to implement this more systematically using custom provider (where querying the CurrentShoppingCart from ECommerceContext could be customized).
Furthermore, we will be probably moving away from session storage to universal Kentico caching mechanisms for shopping cart object in next version, so it should be easier to cover similar external shoping cart update scenarios in the future.
Regards,
Zdenek