Kenji Hashimoto

Delete PI Data Archive tag values by PIWebAPI using IIS sample

Blog Post created by Kenji Hashimoto on Jun 15, 2017

Previously I created delete events sample by AFSDK.

Delete PI Data Archive tag values by AFSDK Sample

I want to achieve the same functions by PI Web API. (I want to delete values from LinuxOS client machine)

I created index.html and js file and put these on the windows machine's IIS. This web page allows users to delete PI tag's events.

I have CentOS (Linux) client machine and could delete events by this web page.

I am using Basic Authentication and that is why I could connect from Linux machine.

This webpage use PI Web API Search query for "Search Tags" button. Using batch request to delete events.

(Remember that PI Web API search crawler could not run by "Basic" authentication.

You can just see the indexed file but the added new points/assets could not be crawled. You can use Indexed Search Shared Index CTP function

to run the PI Web API with Basic authentication and updating PI Web API Search Crawler)


The files are following codes.


<!DOCTYPE html>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
  <meta name="viewport" content="width=400"/>
  <script type="text/javascript" src=""></script>
  <script src="app.js"></script>
  <title>Delete Tag Events</title>  

<body ng-app="myApp">
    <div ng-controller="MyCtrl" >
        Search Tag Name : <input type="Text" ng-model="tagmask"><br>
        <button class="test" type="button" ng-click="searchtags()">
        <span class="ng-scope">Search Tags</span>
        <div ng-show="TagSearchResult">
            Search Tag Results:<br>
            <div ng-repeat="item in search_items">
            Start Time : <input type="Text" ng-model="starttime"><br>
            End Time : <input type="Text" ng-model="endtime"><br>
            Max delete event count : <input type="Text" ng-model="maxdelevnum" style="width:40px;">
            <button class="test" type="button" ng-click="delete()">
            <span class="ng-scope">Delete Events</span>
        <div ng-show="DelResult">
            Delete Event Results:<br>
            <div ng-repeat="item in del_items">
                : Deleted {{item.Content.Count}} events


app.js (Please change var servername ="localhost"; part to your PI Web API Server name. Adding if you want to use Basic authentication, please enter XXXXX part too.)

angular.module('myApp', [])
.controller('MyCtrl', ['$scope','$http', function($scope,$http) {
    //Set PIWebAPI Server Name
    var servername ="localhost";
    //Basic Authentication
    //put the 64bit encoded (username:password) to the XXXXXXXXXXXXXXXXXXXXXXXXXXXXX for using Basic Authentication.
    $http.defaults.headers.common['Authorization'] = "Basic " + "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
    $http.defaults.headers.common['Content-Type'] = 'application/json';
    // Default values
    $scope.tagmask = "sin*";
    $scope.starttime = "*-8h";
    $scope.endtime = "*";
    $scope.maxdelevnum = "50000";
    $scope.TagSearchResult = false;
    $scope.DelResult = false;
    //"Search Tags" Button
    $scope.searchtags= function(){
        var batch_searchtags = {  
            "searchTags": {  
                "Method": "GET",  
                "Resource": "https://" + servername + "/piwebapi/search/query?q=name:"+$scope.tagmask +"&fields=Name"
        $"https://" + servername + "/piwebapi/batch", batch_searchtags, {}).success(function(response) {
            $scope.search_items = response.searchTags.Content.Items;
        //display the results
        $scope.TagSearchResult = true;

    //"Delete Events Button
    $scope.delete= function(){
        var batch_data = {
            "searchTags": {  
                "Method": "GET",  
                "Resource": "https://" + servername + "/piwebapi/search/query?q=name:" + $scope.tagmask + "&fields=Name;Links"
            "getTagInfo": {  
                "Method": "GET",  
                "RequestTemplate" : {"Resource": "{0}?selectedfields=Name;Links.RecordedData"},
                "ParentIds": ["searchTags"],  
                "Parameters": ["$.searchTags.Content.Items[*].Links.Self"]  
                "Method" : "GET",
                "RequestTemplate" : {
                    "Resource": "{0}?maxCount=" + $scope.maxdelevnum + "&selectedfields=Items.Timestamp;Items.Value&StartTime="
                     + $scope.starttime + "&EndTime=" + $scope.endtime 
                "ParentIds": ["getTagInfo"],
                "Parameters" : ["$.getTagInfo.Content.Items[*].Content.Links.RecordedData"]
        $"https://" + servername + "/piwebapi/batch", batch_data, {}).success(function(response) {
            $scope.del_items = response.getTagInfo.Content.Items;
            //Tags loop
            var del_data_body="";
            for (var i = 0; i < response.getTagInfo.Content.Items.length; i++) {
                $scope.del_items[i].Content.Count = response.getData.Content.Items[i].Content.Items.length;
                del_data_body= response.getData.Content.Items[i].Content.Items;
                var tagaddress = response.getTagInfo.Content.Items[i].Content.Links.RecordedData + "?updateOption=remove";
                $, del_data_body, {}).success(function(response) {
                    //alert("success to delete");
        //display the results
        $scope.DelResult = true;


Remember that deleting events affects PI Data Archive machine's performance.

Also I found that trying to delete over 200,000 events, I got an error.

However, I could delete 100,000 events from CentOS7 (Firefox) browser.


The source code is on the Github.

GitHub - kenji0711/PIWebAPIDeleteEvents


If your PI Data Archive is test/ developing environment, you can enjoy it.

For running these kind of custom application tools that use PI Web API/ AFSDK etc... to the production environment, the end user needs to have PSA license.

I worked with Jerome Lefebvre to create this example.