Deleting features

While add_features() and update_features() had a very similar syntax, delete_features() has a somewhat different interface. We have 3 different ways in which we can delete features. Here we will explore only two of them.

library(arcgis)
set_arc_token(auth_code())

nc_url <- "https://services1.arcgis.com/hLJbHVT9ZrDIzK0I/arcgis/rest/services/North%20Carolina%20SIDS/FeatureServer/0" 

nc <- arc_open(nc_url)

We can delete features based on object IDs or a SQL where clause. Let’s explore deleting features based on object IDs. To do so, we need to pass the FeatureLayer obejct as the first argument to delete_features(). The second argument is a numeric vector of the IDs we want to delete. The ID 101 is the new feature that we created.

delete_res <- delete_features(nc, object_ids = 101)
delete_res
$deleteResults
  objectId uniqueId globalId success
1      101      101       NA    TRUE

We can check to see if the delete worked by refreshing the layer and seeing the count that is printed out.

refresh_layer(nc)
<FeatureLayer>
Name: North Carolina SIDS
Geometry Type: esriGeometryPolygon
CRS: 4267
Capabilities: Create,Delete,Query,Update,Editing

Alternatively, we can delete features based on a where clause. Say we wanted to delete all of the features where the BIR74 value was less than 1000. We can accomplish this using a where clause.

delete_res <- delete_features(nc, where = "BIR74 < 1000")
delete_res
$deleteResults
   objectId uniqueId globalId success
1         2        2       NA    TRUE
2         4        4       NA    TRUE
3         7        7       NA    TRUE
4         8        8       NA    TRUE
5         9        9       NA    TRUE
6        20       20       NA    TRUE
7        21       21       NA    TRUE
8        22       22       NA    TRUE
9        32       32       NA    TRUE
10       35       35       NA    TRUE
11       38       38       NA    TRUE
12       44       44       NA    TRUE
13       45       45       NA    TRUE
14       56       56       NA    TRUE
15       58       58       NA    TRUE
16       59       59       NA    TRUE
17       73       73       NA    TRUE
18       77       77       NA    TRUE
19       78       78       NA    TRUE
20       80       80       NA    TRUE
21       83       83       NA    TRUE
22       87       87       NA    TRUE
23       90       90       NA    TRUE

Successful deletes! Again, we can check to see the new count using refresh_layer().

refresh_layer(nc)
<FeatureLayer>
Name: North Carolina SIDS
Geometry Type: esriGeometryPolygon
CRS: 4267
Capabilities: Create,Delete,Query,Update,Editing

Lastly, if you want to delete every single feature. We can take advantage of the where clause again. If we set where = "1 = 1" that will evaluate TRUE for every single feature.

delete_res <- delete_features(nc, where = "1 = 1")
delete_res
$deleteResults
   objectId uniqueId globalId success
1         1        1       NA    TRUE
2         3        3       NA    TRUE
3         5        5       NA    TRUE
4         6        6       NA    TRUE
5        10       10       NA    TRUE
6        11       11       NA    TRUE
          ... Truncated ...
refresh_layer(nc)
<FeatureLayer>
Name: North Carolina SIDS
Geometry Type: esriGeometryPolygon
CRS: 4267
Capabilities: Create,Delete,Query,Update,Editing

Note that you shuold use truncate_layer() instead of delete_features(x, where = "1 = 1").

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.