AngularJS: How to clear query parameters in the URL?

My AngularJS application needs to have access to the user's LinkedIn profile. In order to do that I need to redirect the user to a LinkedIn URL which contains a callback redirect_uri parameter which will tell LinkedIn to redirect the user back to my webapp and include a "code" query param in the URL. It's a traditional Oauth 2.0 flow.

Everything works great except that LinkedIn redirects the user back to the following URL:


I would like to remove ?code=XXX&state=YYY from the URL in order to make it clean. The user does not need to see the query parameters I received from LinkedIn redirect.

I tried $location.absUrl($location.path() + $location.hash()).replace(), but it keep the query params in the URL.

I am also unable to extract the query parameters, e.g. "code", using ($ It seems like having ? before # in the URL above is tricking Angular.


I use

$'key', null)

As this not only deletes my key but removes it from the visibility on the URL.

I ended up getting the answer from AngularJS forum. See this thread for details

The link is to a Google Groups thread, which is difficult to read and doesn't provide a clear answer. To remove URL parameters use


To remove ALL query parameters, do:


To remove ONE particular query parameter, do:

$'myQueryParam', null);

To clear an item delete it and call $$compose

    if ($location.$$search.yourKey) {
        delete $location.$$search.yourKey;

derived from angularjs source :

You can delete a specific query parameter by using:

delete $location.$$search.nameOfParameter;

Or you can clear all the query params by setting search to an empty object:

$location.$$search = {};

At the time of writing, and as previously mentioned by @Bosh, html5mode must be true in order to be able to set $ and have it be reflected back into the window’s visual URL.

See for more info.

But if html5mode is true you can easily clear the URL’s query string with:




This will also alter the window’s visual URL.

(Tested in AngularJS version 1.3.0-rc.1 with html5Mode(true).)

Need to make it work when html5mode = false?

All of the other answers work only when Angular's html5mode is true. If you're working outside of html5mode, then $location refers only to the "fake" location that lives in your hash -- and so $ can't see/edit/fix the actual page's search params.

Here's a workaround, to be inserted in the HTML of the page before angular loads:

  if ("code=")){
    var newHash = "/after-auth" +;
    if (window.history.replaceState){
      window.history.replaceState( {}, "", window.location.toString().replace(, ""));
    window.location.hash = newHash;

Just use


Instead of


If you want to move to another URL and clear the query parameters just use:


If you are using routes parameters just clear $routeParams

$routeParams= null;

How about just setting the location hash to null


if you process the parameters immediately and then move to the next page, you can put a question mark on the end of the new location.

for example, if you would have done $location.path('/nextPage');

you can do this instead: $location.path('/nextPage?');

I've tried the above answers but could not get them to work. The only code that worked for me was $ = ''

I can replace all query parameters with this single line: ${}); Easy to understand and easy way to clear them out.

The accepted answer worked for me, but I needed to dig a little deeper to fix the problems with the back button.

What I noticed is that if I link to a page using <a ui-sref="page({x: 1})">, then remove the query string using $'x', null), I don't get an extra entry in my browser history, so the back button takes me back to where I started. Although I feel like this is wrong because I don't think that Angular should automatically remove this history entry for me, this is actually the desired behaviour for my particular use-case.

The problem is that if I link to the page using <a href="/page/?x=1"> instead, then remove the query string in the same way, I do get an extra entry in my browser history, so I have to click the back button twice to get back to where I started. This is inconsistent behaviour, but actually this seems more correct.

I can easily fix the problem with href links by using $'x', null).replace(), but then this breaks the page when you land on it via a ui-sref link, so this is no good.

After a lot of fiddling around, this is the fix I came up with:

In my app's run function I added this:

$rootScope.$on('$locationChangeSuccess', function () {
    $rootScope.locationPath = $location.path();

Then I use this code to remove the query string parameter:

$'x', null);

if ($location.path() === $rootScope.locationPath) {

Need Your Help

window.blur() - what am I doing wrong


I'm trying to unfocus a window when it's opened, the situation is:

take multiple images into single image

c# html image

Is there a way of taking 3 images and combinging them into one image. I would like to do this and share on facebook and other social sites. Thanks for any advice.