This depends a lot on your business policy, especially when it comes to whether or not to exclude data periodically. There are business models that do not exclude and there are others that exclude (often a wrong choice). I prefer to never delete anything. When you need to remove something irrelevant, but important for a future search, move this data to another database or archive table, history, etc.
On how to reference a user to the cart, you can do this with the ID of the customer who is logged in, as mentioned. However, for non-logged in users, you can use a cookie with an ID that links you to that information.
It is recommended that you save the data from the cart, even that data from users not logged in because it is very useful to analyze the behavior of users, who abandons the cart, at what point the cart was abandoned, what users normally insert in the cart, quantity, value, dates, etc. All this is very important and interesting to direct marketing campaigns and logistics itself.
Another factor is that it makes it more user friendly when you have the cart saved. Many times you may happen to lose the connection and the user wants to come back to the store later. Then you won’t have the trouble of refilling the cart. When the user returns and sees that the cart has been saved, the probability of making the purchase is greater. Otherwise, he’ll feel like he’s going to have to work extra hard at redoing the purchase, discouraging him and even giving up the purchase.
These are the main and most obvious points of why save cart data.
Of course, over time the volume of data will begin to weigh heavily. At this point you and/or the business owners decide what to do with old data. Whether to delete or not, or just move to a history archive, this is a particular decision. As I mentioned above, I prefer to exclude nothing. Even if it seems to be junk, someday in the future you may need this data, either for logistics or to analyze traces of malicious users, for example.