I recently deployed an API with an SQL-database to Azure, which has 2GB of space. Now, I was wondering if it was possible to check (with EntityFramework Core) if the database is reaching it's space limit, and remove the five oldest entries. Is this possible or should I just alter the settings on Azure?
Asked
Active
Viewed 128 times
2
-
Be aware, that deleting records doesn't necessarily free space. DBs tend to keep allocated space for further use. -- to get the information, you might want to look at: https://stackoverflow.com/questions/18014392/select-sql-server-database-size – Corak May 21 '19 at 09:25
1 Answers
1
It's possible that even if you remove 5 entries (or any number really), that you are still out of space. There is just no way of knowing how many records to remove to solve your problem (and nor should there be).
Even if you stumble across something that works today, you have no guarantee that it will work for the next version of SQL-Azure.

Neil
- 11,059
- 3
- 31
- 56
-
Okay, I understand, but what is then the best thing to do if I want to stay below the 2GB? Simply put I want to, if the DB is almost full, add the new element and remove the oldest. Can't this be done? – Acromentula May 21 '19 at 09:31
-
Lets try another angle. *Why* is the database full? If you can delete records when it's 'full', why not just remove those records anyway? Maybe every day, remove records that are older than 1 week? – Neil May 21 '19 at 09:37
-
I like this idea, but I would rather remove the records which are something like 3-6 months old. I don't know how I can pull this off (I'm rather new to this). Is this possible with EF Core? – Acromentula May 21 '19 at 09:43
-
1It's simple to do if your records have a insert/created date column. Then you can just `var oldies = _dbContext.YourTable.Where(x=>x.CreateDate <= DateTime.Now.AddMonths(-6)); _dbContext.YourTable.RemoveRange(oldies);` – Neil May 21 '19 at 13:55