Skip to content

Instantly share code, notes, and snippets.

@rikkit
Created September 3, 2015 13:22
Show Gist options
  • Save rikkit/e0ae200904569a72c181 to your computer and use it in GitHub Desktop.
Save rikkit/e0ae200904569a72c181 to your computer and use it in GitHub Desktop.
delete items
# Delete items using a query
#
# -------------
$eshost = "http://LONAPP101.uklive.exabre.co.uk:50402"
$index = "live_20_34_item"
$type = "statiogn"
$query = '
{
"fields": [],
"size": 100,
"query": {
"filtered": {
"filter": {
"term": {
"Locales.Invariant.Metadata.@@station.@publisher": "abc"
}
}
}
}
}
'
# -------------
# proxy for monitoring progress, set to a url, or to $null to disable
#$proxy = $null
$proxy = 'http://127.0.0.1:8858'
# -------------
echo "This script will delete all items matching:"
echo $eshost/$index/$type
echo $query
# TODO confirmation
$choice = ""
while ($choice -ne "ok")
{
$choice = read-host "Type 'ok' to continue or Ctrl-C to exit"
}
if ($choice -ne "ok") { return }
$totalProcessed = 0
while (1) {
$processedThisBatch = 0
$ids = @()
# the below query selects what will be deleted
@($(irm "$eshost/$index/$type/_search" -Proxy $proxy -Method POST -Body $query).hits.hits `
| foreach -process {
$processedThisBatch += 1
$ids += $_._id
})
if ($ids.Count -gt 0)
{
# build up the bulk json to delete all the duplicate items for this url
$bulks = @()
foreach ($id in $ids) {
$bulk = @"
{ "delete": { "_index": "$index", "_type": "$type", "_id": "$id" } }
"@
$bulks += $bulk
}
$bulks += [Environment]::NewLine
$bulkBody = $bulks -join [Environment]::NewLine | Out-String
# delete the items...
$(irm "$eshost/_bulk" -Method Post -Body $bulkBody -Proxy $proxy) | Out-Null
echo "Deleted $processedThisBatch"
# echo $ids
}
$totalProcessed += $totalProcessed
if ($processedThisBatch -lt 1) { break }
}
echo "Deleted $totalProcessed for query $query"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment