Home » AWS » AWS CloudSearch – clear domain using PowerShell

AWS CloudSearch – clear domain using PowerShell

Amazon Web Services CloudSearch is a search engine and it’s offered in a SaaS model (as well as Elasticsearch what I’ve already explained here). I’m currently writing an article about it so I’ll skip the way how to set it up and how it works. Unfortunately, CloudSearch isn’t free. It doesn’t cost much if you take a look at everything what you get. For more information on pricing check here.

There’s few issues with CloudSearch. One of them is the fact that there’s no easy way to clear search domain. AWS CLI has no such feature. At StackOverflow, I managed to find scripts for Java, NodeJS, PHP or bash.

Clear search domain with PowerShell

On daily basis for scripting purposes I use PowerShell. That’s why I wanted to have a script which would quickly clear CloudSearch domain. However, the use of PowerShell with AWS requires AWS Tools for Windows PowerShell. You may get it here.

Assuming, you’ve got access to modify and query your domain, next step is to obtain Search Url and Document Url from AWS console and replace it in the script below.

$searchUrl = '[SEARCH_URL]'
$documentUrl = '[DOCUMENT_URL]'
$parser = 'Lucene'
$querySize = 500

function Get-DomainHits()
{
    (Search-CSDDocuments -ServiceUrl $searchUrl -Query "*:*" -QueryParser $parser -Size $querySize).Hits;
}

function Get-TotalDocuments()
{
    (Get-DomainHits).Found
}

function Delete-Documents()
{
    (Get-DomainHits).Hit | ForEach-Object -begin { $batch = '[' } -process { $batch += '{"type":"delete","id":' + $_.id + '},'} -end { $batch = $batch.Remove($batch.Length - 1, 1); $batch += ']' }
       
    Try
    {
        Invoke-WebRequest -Uri $documentUrl -Method POST -Body $batch -ContentType 'application/json'
    }
    Catch
    {
        $_.Exception
        $_.Exception.Message
    }
}

$total = Get-TotalDocuments
while($total -ne 0)
{
    Delete-Documents

    $total = Get-TotalDocuments

    Write-Host 'Documents left:'$total
    # Sleep for 1 second to give CS time to delete documents
    sleep 1
}

 

Then just run the script. Results on the screen (not yet finished):

AWS CloudSearch clear domain

You may find this script on GitHub.

Published by

Mateusz Pustelak

Software Developer with several years of commercial experience, TDD practitioner, DDD/CQRS fan. Currently working for Universal Music Group in London.

94 thoughts on “AWS CloudSearch – clear domain using PowerShell”

  1. Couple issues I’ve had using your powershell script that may help others, that you probably already know.

    The $documentUrl needs needs to include “/2013-01-01/documents/batch” from just the base url found in cloudsearch. Second, if your ID is not solely numerical it needs to be encapsulated with quotes on line 18.

  2. Thank your valuable content.we are very thankful to you.one of the recommended blog.which is very useful to new learners and professionals.content is very useful for hadoop learners

  3. Great post i must say and thanks for the information. Education is definitely a sticky subject. However, is still among the leading topics of our time. I appreciate your post and look forward to more.

  4. Excelr is providing emerging & trending technology training, such as for data science, Machine learning, Artificial Intelligence, AWS, Tableau, Digital Marketing. Excelr is standing as a leader in providing quality training on top demanding technologies in 2019. Excelr`s versatile training is making a huge difference all across the globe. Enable ?business analytics? skills in you, and the trainers who were delivering training on these are industry stalwarts. Get certification on
    aws training institute in hyderabad”
    and get trained with Excelr.

  5. This is a nice article here with some useful tips for those who are not used-to comment that frequently. Thanks for this helpful information I agree with all the points you have given to us. I will follow all of them.

  6. Innomatics Research Labs, being one of the best AWS training institute in Hyderabad has also made sure to certify its students with an globally approved certification in AWS which would enhance and open all the biggest career door for all the students who opt this course. Amazon Web Services or AWS training in hyderabad at Innomatics research labs is a secure cloud services platform, offering compute power, database storage, content delivery and other functionality to help businesses scale and grow. According some studies Amazon Web Services or AWS is going to have explosive growth rate in Indian market over other cloud technologies.

  7. Usually, I never comment on blogs but your article is so convincing that I never stop myself to say something about it. I really like this post and Enjoyed reading the article above, really explains everything in detail, the article is very interesting and effective. I am also providing coaching on angular js Angular training in Hyderabad just go through the link

  8. Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.

  9. Get real time project based and job oriented Salesforce training India course materials for Salesforce Certification with securing a practice org, database terminology, admin and user interface navigation and custom fields creation, reports & analytics, security, customization, automation and web to lead forms.  

  10. I will really appreciate the writer’s choice for choosing this excellent article appropriate to my matter.Here is deep description about the article matter which helped me more.
    PMP Certification Pune
    I think this is an informative post and it is very useful and knowledgeable. therefore, I would like to thank you for the efforts you have made in writing this article.

  11. PMP Certification Pune
    I think this is an informative post and it is very useful and knowledgeable. therefore, I would like to thank you for the efforts you have made in writing this article.
    Great post i must say and thanks for the information. Education is definitely a sticky subject. However, is still among the leading topics of our time. I appreciate your post and look forward to more.

  12. I will really appreciate the writer’s choice for choosing this excellent article appropriate to my matter.Here is deep description about the article matter which helped me more.
    PMP Certification Pune
    You completely match our expectation and the variety of our information.. I have express a few of the articles on your website now, and I really like your style of blogging. I added it to my favorite’s blog site list and will be checking back soon…

  13. Good Post! , it was so good to read and useful to improve my knowledge as an updated one, keep blogging.After seeing your article I want to say that also a well-written article with some very good information which is very useful for the readers….thanks for sharing it and do share more posts like this.

  14. Good to become visiting your weblog again, it has been months for me. Nicely this article that i’ve been waited for so long. I will need this post to total my assignment in the college, and it has exact same topic together with your write-up. Thanks, good share.
    data science training

  15. They’re produced by the very best degree developers who will be distinguished for your polo dress creating. You’ll find polo Ron Lauren inside exclusive array which include particular classes for men, women. PMP Certification Pune
    I am genuinely thankful to the holder of this web page who has shared this wonderful paragraph at at this place

  16. They’re produced by the very best degree developers who will be distinguished for your polo dress creating. You’ll find polo Ron Lauren inside exclusive array which include particular classes for men, women. PMP Certification Pune
    Well we really like to visit this site, many useful information we can get here.

  17. I feel really happy to have seen your webpage and look forward to so many more entertaining times reading here. Thanks once more for all the details. PMP Certification Pune
    Well we really like to visit this site, many useful information we can get here.
    I am always searching online for articles that can help me. There is obviously a lot to know about this. I think you made some good points in Features also. Keep working, great job !

Leave a Reply

Your email address will not be published. Required fields are marked *

*