How to empty the cart automatically before adding a new item to the cart?
You can clear the cart using the following function

uc_cart_empty(uc_cart_get_id());


Use the above function in your module when you want clear cart. 
To empty the cart automatically before adding a new item to the cart call the function in hook_add_to_cart(). 

eg: 

function MODULE_NAME__add_to_cart($nid, $qty, $data) {
  uc_cart_empty(uc_cart_get_id());
  drupal_set_message("Previous item cleared");
}