Using the commands I’ve created so far you can now create audiences, add rules, and display those rules all via STSADM. If you’re like me and you do a lot of testing of this stuff before you push out the final version then you’ll want a way to also easily delete audiences that you’ve created. Of course you could easily do this via the browser but what if you’ve added complex rules to your audience using my gl-addaudiencerule command? You’ll find that you can’t delete it from the browser. To deal with this I’ve created an additional command called, simply enough, gl-deleteaudience.

One thing I added to the command was an option to delete only the rules and not the audience itself. This allows you to essentially revert the audience back to a pure state so that you can manage it via the browser if you so desire. The code to delete an audience is real simple – essentially one line: manager.Audiences.Remove(audience.AudienceID); – the rest is just error handling and clearing of the rules:

 1/// <summary>
 2/// Deletes the specified audience or all audience rules for the specified audience.
 3/// </summary>
 4/// <param name="sspName">Name of the SSP.</param>
 5/// <param name="audienceName">Name of the audience.</param>
 6/// <param name="deleteRulesOnly">if set to <c>true</c> [delete rules only].</param>
 7private void Delete(string sspName, string audienceName, bool deleteRulesOnly)
 8{
 9    ServerContext context;
10    if (string.IsNullOrEmpty(sspName))
11        context = ServerContext.Default;
12    else
13        context = ServerContext.GetContext(sspName);
14 
15    AudienceManager manager = new AudienceManager(context);
16 
17    if (!manager.Audiences.AudienceExist(audienceName))
18    {
19        throw new SPException("Audience name does not exist");
20    }
21 
22    Audience audience = manager.Audiences[audienceName];
23 
24    if (audience.AudienceRules != null && deleteRulesOnly)
25    {
26        audience.AudienceRules = new ArrayList();
27        audience.GroupOperation = AudienceGroupOperation.AUDIENCE_OR_OPERATION;
28        audience.Commit();
29        return;
30    }
31    if (!deleteRulesOnly)
32        manager.Audiences.Remove(audience.AudienceID);
33}

The help for the command is shown below:

C:\>stsadm -help gl-deleteaudience

stsadm -o gl-deleteaudience


Deletes an audience.  To delete only the rules associated with the audience pass in the -rulesonly parameter.

Parameters:
        -name <audience name>
        [-ssp <SSP name>]
        [-rulesonly (delete only the rules)]

The following table summarizes the command and its various parameters:

Command NameAvailabilityBuild Date
gl-deleteaudienceMOSS 2007Released: 8/6/2008
Parameter NameShort FormRequiredDescriptionExample Usage
namenYesThe name of the audience to delete.-name "IT", -n "IT"
sspNoThe name of the SSP that the audience is associated with. If omitted the default SSP will be used.-ssp SSP1
-rulesonlyroNoIf specified then only the audience rules will be deleted and not the audience itself.-rulesonly, -ro

The following is an example of how to delete an audience:

stsadm -o gl-deleteaudience -name "IT"

The following is an example of how to delete only the rules associated with an audience

stsadm -o gl-deleteaudience -name "IT" -rulesonly