There are many reasons why you’d need to delete old data from a table within a database, some of which include;
# GDPR (adhering to data retention policies)
# Ever increasing tables becoming tougher to manage (e.g. a replication refresh is taking 2 days)
# SQL Server Express limitations (10GB max database size)
# The table is storing application errors & logs and you’ve been told it’s there to stay
Maybe it’s a one-off job for you, and the deletions can be done within a days work. Or, you might find yourself dealing with very large tables, deleting daily during a small off-peak window over the course of however long… this is a task that can come along with many variations, and if asked the question, how would you do it yourself? Well, “it depends!”.
I’ve run through a simple version of the task below, which also summarises a bunch of my other recent tech posts too. All links below are internal.
Deleting Data in SQL Server
2. Look for one of the larger tables and pick your fancy.
3. Get row counts by date & export for verification later. Or, for this example, I’m storing the info into a temp table as the deletion won’t take long, my session will still be active.
5. Perform new row counts and compare against our previous counts.
Plus a few additional checks to verify.
Looks good to me! All data on orders beyond 7 years (1 January 2012 00:00) has been deleted.
When the initial task is done and all tables are meeting retention periods, we’d then need to consider things like scheduled SQL Agent jobs to maintain the tables every month.