How do I reclaim space in SQL Server?
As time goes by, the disk space requirements for your database may fluctuate greatly. You can set SQL Server to automatically expand a database when it requires more space; and you can also set it to "shrink" when it can reclaim allocated but unused space. Sometimes it makes sense to enable autogrow and autoshrink events, and sometimes it doesn't. (See KB #315512 for information about enabling each of these options.)
Shrinking Data Files (*.MDF / *.NDF)
If you haven't enabled the auto-shrink setting, there may be times when your database has been cleared of significant amounts of data, and you'd like to reclaim the space on disk that the partially empty MDF file is taking up. To do this, you can use the DBCC SHRINKDATABASE command. See this MSDN entry for the technical documentation on the DBCC SHRINKDATABASE or DBCC SHRINKFILE commands:
(A lot of people assume that if you delete a lot of rows or drop a table, that the database size will shrink automatically, but this is not the case. In fact, it is rare that you would want to do this; see Tibor's article for more information.)
And see these articles for more information on shrinking SQL Server databases:
SQL Server Architecture: Shrinking Datab...
Creating and Maintaining Databases: Shri...
Shrinking Log Files (*.LDF)
Another problem you might be having is that the transaction log is filling up too quickly, or staying far too large.
See KB #110139 to understand why the transaction log might fill up.
See KB #317375 for information about full or unexpectedly growing log files.
See KB #873235 for information about preventing unexpected log file growth.
Note that the transaction log is expected to shrink and grow depending on what kind of work you're doing in your database, so the size it expands to might be normal. In this case, shrinking it—while recovering disk space—might actually hurt performance... because the log might have to expand again the next time such a transaction runs, but the database is first going to have to expand the file to make room.
For information on how and why the log file grows and empties, based on your recovery model and backup strategy, see the following article:
SQL Server 2000 Backup and Restore
Depending on your recovery model, you might be able to simply run this command to manually truncate the log:
This empties the log file but does not reduce its size. You will then want to use DBCC SHRINKFILE to reduce the size of the individual LDF file(s). See this MSDN article for more information on shrinking a transaction log:
SQL Server Architecture: Shrinking the T...
For official Knowledge Base information on shrinking the log, see KB #256650 (SQL Server 7.0 / MSDE 1.0) and KB #272318 (SQL Server 2000 / MSDE 2000).
Moving Data / Log Files
If you find that your data and log files are outgrowing their disk(s), see KB #224071 for information about altering file locations using sp_detach_db / sp_attach_db. You might solve your problem simply by separating the data files from the log files and splitting them between drives; this will often yield better performance as well, since the two resources aren't contending for disk i/o.
For some other tips on recovering space or alleviating 'out of space' error messages, see some of the suggestions we have for tempdb (which you could apply to any SQL Server database) in Article #2446.
Related ArticlesAre there tools available for auditing changes to SQL Server data?
Can I create an index on a BIT column?
Can I have optional parameters to my stored procedures?
Can I implement an input mask in SQL Server?
Can I make SQL Server format dates and times for me?
Can I start IDENTITY values at a new seed?
Can SQL Server tell me which row was inserted most recently?
How can I learn more about undocumented SQL Server stored procedures?
How can I make my SQL queries case sensitive?
How do I audit changes to SQL Server data?
How do I connect to a non-default instance of SQL Server?
How do I connect to SQL Server on a port other than 1433?
How do I create a cross-tab (or "pivot") query?
How do I determine if a table exists in a SQL Server database?
How do I drop a SQL Server database?
How do I find all the available SQL Servers on my network?
How do I get a list of SQL Server tables and their row counts?
How do I get rid of Named Pipes / DBNMPNTW errors?
How do I get the correct date/time from the msdb.sysjob* tables?
How do I get the nth row in a SQL Server table?
How do I get the result of dynamic SQL into a variable?
How do I handle REPLACE() within an NTEXT column in SQL Server?
How do I hide system tables in SQL Server's Enterprise Manager?
How do I know which version of SQL Server I'm running?
How do I limit the number of rows returned in my resultset?
How do I load text or csv file data into SQL Server?
How do I manage changes in SQL Server objects?
How do I monitor SQL Server performance?
How do I prevent linked server errors?
How do I recover data from SQL Server's log files?
How do I search for special characters (e.g. %) in SQL Server?
How do I start SQL Server Agent from ASP?
How do I time my T-SQL code?
How do I upsize from Access to SQL Server?
How do I upsize my MSDE database to full-blown SQL Server 2000?
How do I use a variable in a TOP clause in SQL Server?
How do I use GETDATE() within a User-Defined Function (UDF)?
How should I store an IP address in SQL Server?
Schema: How do I find all the foreign keys in a database?
SQL Server & MSDE
What are reserved Access, ODBC and SQL Server keywords?
What are the capacities of Access, SQL Server, and MSDE?
What are the main differences between Access and SQL Server?
What do I need to know about SQL Server 2000 SP4?
Where else can I learn about SQL Server?
Where is SP4 for SQL Server 2000?
Why am I having problems with SQL Server 2000 SP3 / SP3a?
Why can't I install SQL Server on Windows Server 2003?
Why can't I install SQL Server on Windows XP?
Why can't I use LIKE '%datepart%' queries?
Why do I get "Login failed for user '\'."?
Why do I get 'object could not be found' or 'invalid object name'?
Why do I get errors about master..spt_values?
Why do I get script errors in Enterprise Manager's 'taskpad' view?
Why do I get SQLSetConnectAttr Failed errors?
Why do I have problems with views after altering the base table?
Why does EM crash when I get an error in a stored procedure?
Why does Enterprise Manager return 'Invalid cursor state'?
Why does my DELETE query not work?
Why does sp_spaceused return inaccurate values?
Why is Enterprise Manager slow at expanding my database list?
Why is my app slow after upgrading from SQL Server 7 to 2000?
Why is tempdb full, and how can I prevent this from happening?
Why should I consider using an auxiliary calendar table?
Why should I consider using an auxiliary numbers table?