FusionAuth
    • Home
    • Categories
    • Recent
    • Popular
    • Pricing
    • Contact us
    • Docs
    • Login

    I want to delete all the users created before a certain time

    Scheduled Pinned Locked Moved
    Q&A
    bulk delete user-api
    1
    2
    3.3k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • danD
      dan
      last edited by

      How can I do so using the api?

      --
      FusionAuth - Auth for devs, built by devs.
      https://fusionauth.io

      1 Reply Last reply Reply Quote 1
      • danD
        dan
        last edited by

        First, make sure you are using the elasticsearch search engine. You can't run this kind of query on the database search engine.

        Then, you want to use a range query: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-query.html

        You want to build your query string. This is a query string that gives you all the users between the two insertInstants on the user object (inclusive):

         {
         	"range": {
         		"insertInstant": {
         			"gte": 1592231314000,
         			"lte": 1592231315735
         		}
         	}
         }
        

        Omit the gte key if you only want users before a certain time.

        Next is to turn it into an API call against the user search API, which means escaping the query's json and turning it into a JSON object suitable for FusionAuth to parse:

        curl -XPOST -H 'Content-type: application/json' -H "Authorization: $API_KEY" 'https://HOSTNAME/api/user/search?' -d ' { "search" : { "query" :  "{\"range\" : { \"insertInstant\" : { \"gte\" : 1592231314000, \"lte\" : 1592231315735}} }"}}'
        

        Finally, after testing to make sure you're getting the users back you want, you can run the bulk delete API, as documented here: https://fusionauth.io/docs/v1/tech/apis/users#bulk-delete-users

        Tested with Elasticsearch 7.

        --
        FusionAuth - Auth for devs, built by devs.
        https://fusionauth.io

        1 Reply Last reply Reply Quote 0
        • First post
          Last post