tags:

views:

696

answers:

3

I got a table where in got allocated space of 3gig but have no rows.
How to remove this allocated space ?

A: 

You can do:

truncate table [table_name]

Then in Sql Server Express Manager, right click on the database and go to tasks->shrink->database/files (do both). Should clear you up.

Jess
I wouldn't shrink the file. It could be a 500 GB database, just happens to have one table that is empty with 3GB reserved.
gbn
..but TRUNCATE is correct though.
gbn
If you shrink the database, you need to rebuild all the indexes due to the fragmentation cauesed by the shrink.
Hakan Winther
A: 

These should do it, they will shrink your database files. If the table is empty the reserved space for the table will be released with the following commands

DBCC SHRINKDATABASE ('DBName')
DBCC SHRINKFILE ('LogicalFileName')
Coentje
If you shrink the database, you need to rebuild all the indexes due to the fragmentation cauesed by the shrink.
Hakan Winther
You are right, your indexes are a mess after shrinking the database but if your desire for freeing up space is large enough and the space gained is enough it is the only way to free up the space
Coentje
A: 

There are some restriction with TRUNCATE TABLE, and if can't do a truncate then you can rebuild your clustered index to free up the allocated space.

You cannot use TRUNCATE TABLE on tables that:

  • Are referenced by a FOREIGN KEY constraint. (You can truncate a table that has a foreign key that references itself.)
  • Participate in an indexed view.
  • Are published by using transactional replication or merge replication.

If the table contains an identity column, the counter for that column is reset to the seed value defined for the column. If no seed was defined, the default value 1 is used. To retain the identity counter, use DELETE instead. If this is the case then you need to reseed the table to continue the identity increment from where it was.

If the table does not have a clustered index you can use TABLOCK hint in your DELETE statement to free up the allocated space.

DELETE FROM table WITH (TABLOCK)

If table is using a clustered index you can rebuild the index to free the space.

Hakan Winther