Skip To Content ArcGIS for Developers Sign In Dashboard

Delete features (feature service)

Delete features from a feature layer


// Copyright 2019 Esri.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an 
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific 
// language governing permissions and limitations under the License.

using Esri.ArcGISRuntime.Data;
using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.UI.Controls;
using Foundation;
using System;
using System.Linq;
using UIKit;

namespace ArcGISRuntimeXamarin.Samples.DeleteFeatures
        "Delete features (feature service)",
        "Delete features from a feature service.",
    public class DeleteFeatures : UIViewController
        // Hold a reference to the MapView.
        private MapView _myMapView;

        // URL to the feature service.
        private const string FeatureServiceUrl = "";

        // Hold a reference to the feature layer.
        private FeatureLayer _damageLayer;

        public DeleteFeatures()
            Title = "Delete features";

        private void Initialize()
            // Create the map with streets basemap.
            _myMapView.Map = new Map(Basemap.CreateStreets());

            // Create the feature table, referring to the Damage Assessment feature service.
            ServiceFeatureTable damageTable = new ServiceFeatureTable(new Uri(FeatureServiceUrl));

            // Create a feature layer to visualize the features in the table.
            _damageLayer = new FeatureLayer(damageTable);

            // Add the layer to the map.

            // Listen for user taps on the map - on tap, a callout will be shown.
            _myMapView.GeoViewTapped += MapView_Tapped;

            // Zoom to the United States.
            _myMapView.SetViewpointCenterAsync(new MapPoint(-10800000, 4500000, SpatialReferences.WebMercator), 3e7);

        private async void MapView_Tapped(object sender, GeoViewInputEventArgs e)
            // Clear any existing selection.

            // Dismiss any existing callouts.

                // Perform an identify to determine if a user tapped on a feature.
                IdentifyLayerResult identifyResult = await _myMapView.IdentifyLayerAsync(_damageLayer, e.Position, 8, false);

                // Do nothing if there are no results.
                if (!identifyResult.GeoElements.Any())

                // Otherwise, get the ID of the first result.
                long featureId = (long) identifyResult.GeoElements.First().Attributes["objectid"];

                // Get the feature by constructing a query and running it.
                QueryParameters qp = new QueryParameters();
                FeatureQueryResult queryResult = await _damageLayer.FeatureTable.QueryFeaturesAsync(qp);
                Feature tappedFeature = queryResult.First();

                // Select the feature.

                // Show the callout.
            catch (Exception ex)
                ShowMessage(ex.ToString(), "There was a problem.");

        private void ShowDeletionCallout(Feature tappedFeature)
            // Create a button for deleting the feature.
            UIButton deleteButton = new UIButton();
            deleteButton.SetTitle("Delete feature", UIControlState.Normal);
            deleteButton.SetTitleColor(View.TintColor, UIControlState.Normal);

            // Handle button clicks.
            deleteButton.TouchUpInside += (o, e) => { DeleteFeature(tappedFeature); };

            // Show the callout.
            _myMapView.ShowCalloutAt((MapPoint) tappedFeature.Geometry, deleteButton);

        private async void DeleteFeature(Feature featureToDelete)
            // Dismiss the callout.

                // Delete the feature.
                await _damageLayer.FeatureTable.DeleteFeatureAsync(featureToDelete);

                // Sync the change with the service.
                ServiceFeatureTable serviceTable = (ServiceFeatureTable) _damageLayer.FeatureTable;
                await serviceTable.ApplyEditsAsync();

                // Show a message confirming the deletion.
                ShowMessage($"Deleted feature with ID {featureToDelete.Attributes["objectid"]}", "Success!");
            catch (Exception ex)
                ShowMessage(ex.ToString(), "Couldn't delete feature");

        private void ShowMessage(string message, string title)
            // Create the alert controller.
            UIAlertController alertController = UIAlertController.Create(title, message, UIAlertControllerStyle.Alert);
            alertController.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));

            // Show the alert.
            PresentViewController(alertController, true, null);

        public override void ViewDidLoad()

        public override void LoadView()
            // Create the views.
            View = new UIView();

            _myMapView = new MapView();
            _myMapView.TranslatesAutoresizingMaskIntoConstraints = false;

            UILabel helpLabel = new UILabel
                Text = "Tap to select a feature for deletion.",
                AdjustsFontSizeToFitWidth = true,
                TextAlignment = UITextAlignment.Center,
                BackgroundColor = UIColor.FromWhiteAlpha(0, .6f),
                TextColor = UIColor.White,
                Lines = 1,
                TranslatesAutoresizingMaskIntoConstraints = false

            // Add the views.
            View.AddSubviews(_myMapView, helpLabel);

            // Lay out the views.


In this topic
  1. Code