---
title: "Invoke-DbaDbDbccCleanTable"
description: "Reclaims disk space from dropped variable-length columns in tables and indexed views"
url: "https://dbatools.io/Invoke-DbaDbDbccCleanTable/"
availability: "Windows, Linux, macOS"
tags: ["DBCC"]
author: "Patrick Flynn (@sqllensman)"
source: "https://github.com/dataplat/dbatools/blob/master/public/Invoke-DbaDbDbccCleanTable.ps1"
last_updated: "2024-01-01"
---

# Invoke-DbaDbDbccCleanTable

## Synopsis

Reclaims disk space from dropped variable-length columns in tables and indexed views

## Description

Executes DBCC CLEANTABLE to reclaim disk space after variable-length columns (varchar, nvarchar, varbinary, text, ntext, image, xml, or CLR user-defined types) have been dropped from tables or indexed views.  
  
When you drop variable-length columns, SQL Server doesn't immediately reclaim the space those columns occupied. This function runs the necessary DBCC command to physically remove that unused space and compact the remaining data, which can significantly reduce table size and improve performance.  
  
The function accepts either table names (like 'dbo.TableName') or table object IDs for processing. You can control the operation through batch processing to minimize impact on production systems, and optionally suppress informational messages for cleaner output.  
  
Read more:  
    - https://docs.microsoft.com/en-us/sql/t-sql/database-console-commands/dbcc-cleantable-transact-sql

## Syntax

```powershell
Invoke-DbaDbDbccCleanTable
    [-SqlInstance] <DbaInstanceParameter[]>
    [[-SqlCredential] <PSCredential>]
    [[-Database] <String[]>]
    [[-Object] <String[]>]
    [[-BatchSize] <Int32>]
    [-NoInformationalMessages]
    [-EnableException]
    [-WhatIf]
    [-Confirm]
    [<CommonParameters>]

```

## Examples

### Example 1: Connects to CurrentDB on instance SqlServer2017 using Windows Authentication and runs the command DBCC...

```powershell
PS C:\> Invoke-DbaDbDbccCleanTable -SqlInstance SqlServer2017 -Database CurrentDB -Object 'dbo.SomeTable'
```

Connects to CurrentDB on instance SqlServer2017 using Windows Authentication and runs the command DBCC CLEANTABLE('CurrentDB', 'dbo.SomeTable') to reclaim space after variable-length columns have  
been dropped.  

### Example 2: Connects to CurrentDB on instance SqlServer2017 using Windows Authentication and runs the command DBCC...

```powershell
PS C:\> Invoke-DbaDbDbccCleanTable -SqlInstance SqlServer2017 -Database CurrentDB -Object 34636372 -BatchSize 5000
```

Connects to CurrentDB on instance SqlServer2017 using Windows Authentication and runs the command DBCC CLEANTABLE('CurrentDB', 34636372, 5000) to reclaim space from table with Table_Id = 34636372  
after variable-length columns have been dropped.  

### Example 3: Connects to CurrentDB on instance SqlServer2017 using sqladmin credential and runs the command DBCC...

```powershell
PS C:\> $cred = Get-Credential sqladmin
PS C:\> Invoke-DbaDbDbccCleanTable -SqlInstance SqlServer2017 -SqlCredential $cred -Database CurrentDB -Object 'dbo.SomeTable'  -NoInformationalMessages
```

Connects to CurrentDB on instance SqlServer2017 using sqladmin credential and runs the command DBCC CLEANTABLE('CurrentDB', 'dbo.SomeTable') WITH NO_INFOMSGS to reclaim space after variable-length  
columns have been dropped.  

### Example 4: Runs the command DBCC CLEANTABLE(&#39;DatabaseName&#39;, &#39;dbo.SomeTable&#39;, 5000) against all databases on Sql1 and...

```powershell
PS C:\> 'Sql1','Sql2/sqlexpress' | Invoke-DbaDbDbccCleanTable -Object 'dbo.SomeTable' -BatchSize 5000
```

Runs the command DBCC CLEANTABLE('DatabaseName', 'dbo.SomeTable', 5000) against all databases on Sql1 and Sql2/sqlexpress.  

### Required Parameters

##### -SqlInstance

The target SQL Server instance or instances.

| Property | Value |
| --- | --- |
| Alias |  |
| Required | True |
| Pipeline | true (ByValue) |
| Default Value |  |

### Optional Parameters

##### -SqlCredential

Login to the target instance using alternative credentials. Accepts PowerShell credentials (Get-Credential).  
Windows Authentication, SQL Server Authentication, Active Directory - Password, and Active Directory - Integrated are all supported.  
For MFA support, please use Connect-DbaInstance.

| Property | Value |
| --- | --- |
| Alias |  |
| Required | False |
| Pipeline | false |
| Default Value |  |

##### -Database

Specifies which databases to include in the clean table operation. Accepts multiple database names.  
When omitted, the operation runs against all accessible databases on the instance.  
Use this to target specific databases where you know variable-length columns have been dropped recently.

| Property | Value |
| --- | --- |
| Alias |  |
| Required | False |
| Pipeline | false |
| Default Value |  |

##### -Object

Specifies the table or indexed view names to clean, or their object IDs for more precise targeting.  
Accepts schema-qualified names like 'dbo.TableName' or numeric object IDs like 34636372.  
This parameter is required since DBCC CLEANTABLE must target specific objects, not entire databases.

| Property | Value |
| --- | --- |
| Alias |  |
| Required | False |
| Pipeline | false |
| Default Value |  |

##### -BatchSize

Controls how many rows are processed per transaction during the clean operation.  
Use smaller batch sizes (like 5000-10000) on production systems to reduce lock duration and transaction log impact.  
When omitted, processes the entire table in a single transaction which is faster but holds locks longer.

| Property | Value |
| --- | --- |
| Alias |  |
| Required | False |
| Pipeline | false |
| Default Value | 0 |

##### -NoInformationalMessages

Suppresses informational messages from the DBCC CLEANTABLE output, showing only errors and warnings.  
Use this in automated scripts or when processing multiple tables to reduce console output volume.  
Equivalent to adding WITH NO_INFOMSGS to the DBCC command.

| Property | Value |
| --- | --- |
| Alias |  |
| Required | False |
| Pipeline | false |
| Default Value | False |

##### -EnableException

By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.  
This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting.  
Using this switch turns this "nice by default" feature off and enables you to catch exceptions with your own try/catch.

| Property | Value |
| --- | --- |
| Alias |  |
| Required | False |
| Pipeline | false |
| Default Value | False |

##### -WhatIf

Shows what would happen if the cmdlet runs. The cmdlet is not run.

| Property | Value |
| --- | --- |
| Alias | wi |
| Required | False |
| Pipeline | false |
| Default Value |  |

##### -Confirm

Prompts you for confirmation before running the cmdlet.

| Property | Value |
| --- | --- |
| Alias | cf |
| Required | False |
| Pipeline | false |
| Default Value |  |

## Outputs

**PSCustomObject**

Returns one object per table or indexed view being cleaned across all specified databases.

**Properties:**

- ComputerName: The computer name of the SQL Server instance
- InstanceName: The SQL Server instance name
- SqlInstance: The full SQL Server instance name (computer\instance format)
- Database: Name of the database containing the object being cleaned
- Object: The table or indexed view name or object ID specified for cleaning
- Cmd: The DBCC CLEANTABLE command that was executed
- Output: DBCC output messages, informational status, or null if operation completed silently

---

Part of [dbatools](https://dbatools.io/), a free and open source PowerShell module for SQL Server administration. Full command index: https://dbatools.io/commands/
