Power Platform Governance: Deleting unwanted environments with PAC CLI
I recently ran a training session where the attendees created 25 trials in our training tenant. While trials expire in 30 days, I started wondering how I could automate the deletion of trials with PAC CLI (“Power Platform Command Line Interface”).
Why that and not Power Automate? Well, to be honest, I just wanted to learn how it’s done. We’re only talking about 25 environments so manually deleting them is faster than with Power Automate. But what about PAC CLI?
Determining what to delete
The first thing that we need to consider is how do we loop through all environments that we want to delete. Looping through them based on their type doesn’t seem to be possible in PAC CLI (correct me if I’m wrong and there’s a way to do it) so looking at their names is a logical option. Ok, so name it is. Now, let’s look at image 1 below to understand how the trial environments are named in the tenant. As we can see, the names are all over. There’s no specific naming convention for them, except how they become named by the platform on create. Field Service Trial is a good example of a platform named trial environment.
Based on the environment names, we can deduce that trials containing the following strings in their names are something we can easily access when using the pac admin list command to list environments:
- trial_
- Field Service Trial
- GovLab2
- ff.training
The PAC CLI script
Let’s look at the actual PAC CLI script for this use case next. Before we move on, I want to give kudos to the one and only Daniel Laskewitz. He actually solved this for me with the help of the Product Group, so huge thanks to the PG as well. Turns out the command pac admin list has an undocumented –json paramenter, which we need for the command to return JSON to PowerShell. So don’t thank me for any of this. Thank Daniel and his peers instead.
Ok, on to the script itself. Let’s look at the script in its entirety first and then dissect it:
$name = "prefixhere";
$environmentsList = (pac admin list --name $name --json | ConvertFrom-Json)
foreach ($env in $environmentsList) {
Write-Output $env.EnvironmentID
# pac admin delete -env $env.EnvironmentID
}
And now the same as an image from VS Code so that we can discuss the lines in the script:
- Declare variable $name for a given string in environment name. Be extra careful here! We will list all environments that contain the string declared in this variable.
- Declare variable $environmentsList that includes:
- List environments with the pac admin list command, based on their –name in variable $name, and return JSON to PowerShell by using –json, and then convert the output to a PowerShell object with | ConvertFrom-Json.
- Run a foreach loop and in that loop, declare variable $env in the previously declared $environmentsList variable.
- Write the output of the previously declared variable $env and include the EnvironmentID property.
- For testing, use # to comment and to prevent an accidental delete! The command pac admin delete with the –env parameter will delete the environment in the iteration of the foreach loop, based on variable and property $env.EnvironmentID.
Delete a trial example 1
Script used:
$name = "Field Service Trial";
$environmentsList = (pac admin list --name $name --json | ConvertFrom-Json)
foreach ($env in $environmentsList) {
Write-Output $env.EnvironmentID
# pac admin delete -env $env.EnvironmentID
}
Result in VS Code:
Example 1 environments in PPAC:
Delete a trial example 2
Script used. Notice the -a operator for asynchronous processing. Deleting environments is slow.
$name = "govlab2";
$environmentsList = (pac admin list --name $name --json | ConvertFrom-Json)
foreach ($env in $environmentsList) {
Write-Output $env.EnvironmentID
# pac admin delete -env $env.EnvironmentID -a
}
Result in VS Code:
Example 2 environments in PPAC:
The pac admin status command will list the status of the async operations:
And there we have it! With this very simple PAC CLI script, environments can be easily deleted. With power comes responsibility so make sure you’re absolute certain you’re looping through the correct environments based on their name! Keep in mind what Learn states about the –name parameter when used with the pac admin list command:
“List all environments that contain given string in their name.”