To decrease the size of a database, use DBCC SHRINKDATABASE.
You cannot add or remove a file while a BACKUP statement is running.
A maximum of 32,767 files and 32,767 filegroups can be specified for each database.
In SQL Server 2005 or later, the state of a database file (for example, online or offline), is maintained independently from the state of the database. For more information, see File States. The state of the files within a filegroup determines the availability of the whole filegroup. For a filegroup to be available, all files within the filegroup must be online. If a filegroup is offline, any try to access the filegroup by an SQL statement will fail with an error. When you build query plans for SELECT statements, the query optimizer avoids nonclustered indexes and indexed views that reside in offline filegroups. This enables these statements to succeed. However, if the offline filegroup contains the heap or clustered index of the target table, the SELECT statements fail. Additionally, any INSERT, UPDATE, or DELETE statement that modifies a table with any index in an offline filegroup will fail.
Moving Files
In SQL Server 2005 or later, you can move system or user-defined data and log files by specifying the new location in FILENAME. This may be useful in the following scenarios:
-
Failure recovery. For example, the database is in suspect mode or shutdown caused by hardware failure
-
Planned relocation
-
Relocation for scheduled disk maintenance
For more information, see Move Database Files.
Initializing Files
By default, data and log files are initialized by filling the files with zeros when you perform one of the following operations:
-
Create a database
-
Add files to an existing database
-
Increase the size of an existing file
-
Restore a database or filegroup
Data files can be initialized instantaneously. This enables for fast execution of these file operations.
Removing a FILESTREAM Container
Even though FILESTREAM container may have been emptied using the “DBCC SHRINKFILE” operation, the database may still need to maintain references to the deleted files for various system maintenance reasons. sp_filestream_force_garbage_collection (Transact-SQL) will run the FILESTREAM Garbage Collector to remove these files when it is safe to do so. Unless the FILESTREAM Garbage Collector has removed all the files from a FILESTREAM container, the ALTER DATABASEREMOVE FILE operation will fail to remove a FILESTREAM container and will return an error. The following process is recommended to remove a FILESTREAM container.
-
Run DBCC SHRINKFILE (Transact-SQL) with the EMPTYFILE option to move the active contents of this container to other containers.
-
Ensure that Log backups have been taken, in the FULL or BULK_LOGGED recovery model.
-
Ensure that the replication log reader job has been run, if relevant.
-
Run sp_filestream_force_garbage_collection (Transact-SQL) to force the garbage collector to delete any files that are no longer needed in this container.
-
Execute ALTER DATABASE with the REMOVE FILE option to remove this container.
-
Repeat steps 2 through 4 once more to complete the garbage collection.
-
Use ALTER Database...REMOVE FILE to remove this container.