Delete a Query Activity verified

Learn how to delete (remove) a Query Activity in Salesforce Marketing Cloud (SFMC) with SSJS (server-side JavaScript). Code snippets include WSProxy and REST API methods.

Core

var customerKey = "S0M3-GU1D-K3Y-G03SR1G4T-H3R3";

var qd = QueryDefinition.Init(customerKey);

var result = qd.Remove();
<script runat="server">

    Platform.Load("core", "1");

	try {

        var customerKey = "S0M3-GU1D-K3Y-G03SR1G4T-H3R3";

        var qd = QueryDefinition.Init(customerKey);

        var result = qd.Remove();

        Write(Stringify(result));
		
	} catch(error) {

        Write(Stringify(error));
        
    }	

</script>
"OK"

WSProxy

var api = new Script.Util.WSProxy();

var objectId = "S0M3-GU1D-K3Y-G03SR1G4T-H3R3";

var result = api.deleteItem("QueryDefinition", { 
    "ObjectID": objectId 
});
<script runat="server">

    Platform.Load("core", "1");

    var api = new Script.Util.WSProxy();

	try {

        var request = api.retrieve("QueryDefinition", ["ObjectID"], {
            Property: "Name",
            SimpleOperator: "equals",
            Value: "MyNewQuery"
        });

        var objectId = request.Results[0].ObjectID;

        var result = api.deleteItem("QueryDefinition", { 
            "ObjectID": objectId 
        });

        Write(Stringify(result));

	} catch(error) {

        Write(Stringify(error));

    }	

</script>
{
    "Status": "OK",
    "RequestID": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
    "Results": [
        {
            "Object": {
                "Name": null,
                "ObjectID": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
                "QueryText": null,
                "TargetType": null,
                "DataExtensionTarget": null,
                "TargetUpdateType": null,
                "FileSpec": null,
                "FileType": null,
                "Status": null,
                "CategoryID": 0,
                "InteractionObjectID": null,
                "Description": null,
                "Keyword": null,
                "Client": null,
                "PartnerKey": null,
                "PartnerProperties": null,
                "CreatedDate": "0001-01-01T00:00:00.000",
                "ModifiedDate": null,
                "ID": 0,
                "CustomerKey": null,
                "Owner": null,
                "CorrelationID": null,
                "ObjectState": null,
                "IsPlatformObject": false
            },
            "StatusCode": "OK",
            "StatusMessage": "QueryDefinition deleted",
            "OrdinalID": 0,
            "ErrorCode": 0,
            "RequestID": null,
            "ConversationID": null,
            "OverallStatusCode": null,
            "RequestType": "Synchronous",
            "ResultType": null,
            "ResultDetailXML": null
        }
    ]
}

REST API

var queryDefinitionId =  "S0M3-GU1D-K3Y-G03SR1G4T-H3R3";

var endpoint = restInstanceUrl + "automation/v1/queries/" + queryDefinitionId;

var request = new Script.Util.HttpRequest(endpoint);
    request.emptyContentHandling = 0;
    request.retries = 2;
    request.continueOnError = true;
    request.setHeader("Authorization", "Bearer " + accessToken);
    request.method = "DELETE";
    request.contentType = "application/json";
    request.encoding = "UTF-8";

var results = request.send();

var result = Platform.Function.ParseJSON(String(results.content));
<script runat="server">

    Platform.Load("core", "1");

    var api = new Script.Util.WSProxy();

    var restInstanceUrl = "https://YOUR_SUBDOMAIN.rest.marketingcloudapis.com/",
        accessToken     = "YOUR_REST_API_TOKEN";
	
	try {

        var name = "MyNewOldQuery";

        var request = api.retrieve("QueryDefinition", ["ObjectID"], {
            Property: "Name", 
            SimpleOperator: "equals",
            Value: name 
        });

        var queryDefinitionId = request.Results[0].ObjectID;

        var endpoint = restInstanceUrl + "automation/v1/queries/" + queryDefinitionId;

        var request = new Script.Util.HttpRequest(endpoint);
            request.emptyContentHandling = 0;
            request.retries = 2;
            request.continueOnError = true;
            request.setHeader("Authorization", "Bearer " + accessToken);
            request.method = "DELETE";
            request.contentType = "application/json";
            request.encoding = "UTF-8";

        var results = request.send();

        var result = Platform.Function.ParseJSON(String(results.content));

        Write(Stringify(result));
		
	} catch(error) {
        Write(Stringify(error));
    }	

</script>
"\"OK\""

Reference

Ressources and references related to the current methods.

SOAP object

Last Updated: