Sql-server – Get MS SQL Server to release disk space

disk-spaceshrinksql server

A poorly managed database table has grown to be enormous. 48+gigs of orphan records. I am trying to clean it up and put my dangerously full hard drive back to a normal state. I will be deleting approx 400 million records from this table. This is running as I type. I notice that I am not seeing any drop in my hard drive space but I am seeing the memory drop on system table queries, I am running to get table size. The database is using "Simple Recovery Model".

There are many questions similar to this with responses saying you need to shrink the database. But they go on to explain how bad/scary this is to do because of fragmented data etc.

  1. Since the database should not be this size. Is it still bad for me to shrink it?
  2. This is a production database. If I shrink it will it cause downtime or lock the database?
  3. In SQL Server Management Studio you have two options for shrink, database or files. Given my situation what would be the best option?
  4. is there a rule on the percentage of free space a DB should have?

Even reading the tag description of shrink makes me not want to do it. Is there another way?

Best Answer

I will be deleting approx 400 million records from this table.

Hopefully, you are doing it in chunks - to avoid bloating transaction log.

notice that i am not see any drop in my hard drive space

You wont, as you have to explicitly shrink the database file to release space. Just deleting the records, SQL server wont release the space back to the OS.

  1. Since the database should not be this size. Is it still bad for me to shrink it?

It is generally a bad practice as databases should be presized properly. If you are 100% sure that your database is NOT going to be again this size, its OK for you to shrink the database (in your scenario).

  1. This is a production Database. If I shrink it will it cause downtime or lock the database?

Shrinking a database is a very IO intensive. Its advisable to shrink using DBCC SHRINKFILE during maintenance window or when there is minimal activity.

  1. In management studio you have two options, Database or Files. Given my situation what would be the best option?

You should use TSQL to do the shrink. (since you asked, shrinking FILE is the way to go instead of database). You can use shrink database in chunks - tsql script to do the shrink in chunks.

Remember that when you shrink, you are fragmenting your indexes.

  1. is there a rule on the percentage of free space a DB should have?

You can refer to Estimating Disk Space Requirements for Databases article. There is no fix rule, you have to take into account of how your database is expected to grow. You also have to take into account, autogrowth of your databases.

References : Why Does the Transaction Log Keep Growing or Run Out of Space?