Deleting a Table in Dataverse using PowerShell

Loading

Microsoft Dataverse allows organizations to store and manage structured data for Power Platform applications. Sometimes, you may need to delete a table (entity) in Dataverse using PowerShell, either for cleanup or restructuring.

This guide provides a step-by-step process to delete a table in Dataverse using PowerShell.


Step 1: Prerequisites

1. Required Permissions

  • You must have System Administrator or Power Platform Admin role in Dataverse.
  • The Microsoft Dataverse API should be enabled for your environment.

2. Install and Import PowerShell Modules

To interact with Dataverse, install and import the necessary PowerShell modules.

# Install Power Platform Administration module
Install-Module -Name Microsoft.PowerPlatform.Administration -Scope CurrentUser -Force

# Install Dataverse Client module
Install-Module -Name Microsoft.PowerPlatform.Cds.Client -Scope CurrentUser -Force

# Import modules
Import-Module Microsoft.PowerPlatform.Administration
Import-Module Microsoft.PowerPlatform.Cds.Client

Step 2: Connect to Dataverse

Option 1: Interactive Login (Prompt for Credentials)

# Connect to Dataverse interactively
$connection = Connect-CdsService -ConnectionString "AuthType=OAuth;Url=https://yourorg.crm.dynamics.com;Prompt=Login"

A sign-in window will appear, prompting you to log in with your Microsoft 365 admin credentials.

Option 2: Using Service Principal (App Registration)

For automated scripts, use an Azure AD App Registration.

# Define connection details
$clientId = "your-app-client-id"
$clientSecret = "your-app-client-secret"
$tenantId = "your-tenant-id"
$orgUrl = "https://yourorg.crm.dynamics.com"

# Create authentication context
$secureSecret = ConvertTo-SecureString $clientSecret -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential ($clientId, $secureSecret)

# Connect to Dataverse
$connection = Connect-CdsService -Url $orgUrl -ClientId $clientId -ClientSecret $secureSecret -TenantId $tenantId

Step 3: List All Tables in Dataverse

Before deleting a table, you should list existing tables to find the correct logical name.

# Retrieve all tables in Dataverse
$tables = Get-CdsTable -Connection $connection

# Display tables
$tables | Select-Object LogicalName, DisplayName

Step 4: Delete a Table in Dataverse

Once you have identified the table you want to delete, follow these steps:

1. Define the Table to Delete

Replace "new_CustomerData" with the logical name of the table you want to delete.

# Specify the logical name of the table to delete
$tableName = "new_CustomerData"

# Delete the table
Remove-CdsTable -Connection $connection -EntityLogicalName $tableName -Confirm:$false
Write-Host "Table '$tableName' has been deleted successfully."

2. Confirm Deletion by Listing Tables Again

# Verify that the table is deleted
$tablesAfterDeletion = Get-CdsTable -Connection $connection
$tablesAfterDeletion | Select-Object LogicalName, DisplayName

Step 5: Publish Customizations

After deleting the table, publish changes to apply them.

Publish-CdsCustomization -Connection $connection
Write-Host "Customizations published successfully."

Step 6: Disconnect from Dataverse

After completing your task, disconnect the session.

Disconnect-CdsService -Connection $connection
Write-Host "Disconnected from Dataverse."

Leave a Reply

Your email address will not be published. Required fields are marked *