How to release space after deleting table data in SQL Server?

There are several methods available in SQL Server to delete table data and release space.

  1. The TRUNCATE TABLE statement removes all data from a table and frees up space. The syntax is as follows:
  2. Clear all data in the table named table_name.
  3. Note: The TRUNCATE TABLE statement is a DDL statement that immediately frees up the space occupied by the table, but it cannot be rolled back.
  4. The DELETE statement is used to remove data from a table, but it does not immediately release space. To release space, you can use one of the following methods:
  5. DBCC SHRINKDATABASE: This command is used to reduce the size of the entire database and free up space. The syntax is as follows:
    DBCC SHRINKDATABASE (database_name);

    Please note that this command will shrink the size of the entire database, not just free up space from a specific table.

  6. DBCC SHRINKFILE: This command is used to reduce the size of a database file and can free up space. The syntax is as follows:
    DBCC SHRINKFILE (logical_file_name, target_size);

    Note: logical_file_name is the logical name of the database file, and target_size is the desired file size.

  7. You can utilize the graphical interface of SSMS (SQL Server Management Studio) to shrink databases or files and free up space by using the “Tasks” -> “Shrink” feature.

Note: It is recommended to back up the database before conducting any space release operations to prevent accidental data loss.

bannerAds