SQL Server – How to Shrink Data File with DBCC SHRINKFILE Command

dbccshrinksql server

I have a datafile with the following parameters:

enter image description here

When I execute DBCC SHRINKFILE (DBAtools_data,1), it shows the following resuls:

enter image description here

I am OK with that. My complaint is about DBCC SHRINKFILE (DBAtools_data,0) command. When I run it nothing changes. I thought that it will shrink the datafile to the last allocated extent, in order words the same result as above. Could somebody clarify it for me?

Best Answer

Without having access to the source code for SQL Server, we can only guess. So,

Edit: 0 means shrink to initial size as per page header. My speculation that it means dummy, below, is incorrect.

SET SPECULATION MODE ON

0 makes no sense. If you want to get rid of the file, you have the EMPTYFILE option. I.e., why would you want to have a file which is 0 MB in size.

I just did a test, and indeed 0 means it doesn't do anything. We could argue that an error messages would be better: "0 MB is not valid file size.", or something like that. But apparently MS decided to leave 0 as a valid valid for the command and implementing it as a dummy.

SET SPECULATION MODE OFF

I suggest you file a request with MS to either document that 0 means "dummy", or behavior change so we'll get an error message (less likely to happen).