3

In the Developer Tools' Console in Chrome, I want to be able to completely delete all variables stored in memory.

For example:

var x = 8;
x // 8
clear()
x // not found, x is not defined

instead every time I try to do a variation of clear() or console.clear(), the variables still exist in memory and I get:

var x = 8;
x // 8
clear()
x // 8

Any help with this would be greatly appreciated.

Steven_
  • 738
  • 2
  • 7
  • 20
  • 1
    Possible duplicate of [Chrome console clear assignment and variables](http://stackoverflow.com/questions/34270829/chrome-console-clear-assignment-and-variables) – iagowp Jan 14 '17 at 21:53
  • You mean clearing the memory of all the recently typed and executed stuff, right? This might be what you are looking for, but the answer is not as simple as executing a command: http://stackoverflow.com/a/21149275/1760313 – Tom Jan 14 '17 at 21:57
  • @Tom something close to that, but is there nothing similar to a `consoleHistory.delete()` command? instead of going through the console GUI to do all that – Steven_ Jan 14 '17 at 21:59
  • or a `delete all` command? – Steven_ Jan 14 '17 at 22:00
  • No, there is `localstorage.removeItem(key)`, but you can't use that to remove the `consoleHistory` key which belongs to another domain (or in this case, the dev tools domain). It would be a security risk if you could in normal situations, obviously the dev tools within the Console tab would be an exception, but that's not been taken into account. – Tom Jan 14 '17 at 22:09
  • You could however right click and click `Clear console history`. It's way faster. – Tom Jan 14 '17 at 22:11
  • Its a shame that `clear console history` isn't available in firefox dev tools 112 – Fishbite Mar 17 '23 at 16:58
  • Reload the browser. – Bello Tomi Jun 12 '23 at 07:22

3 Answers3

3

Clearing the console history is easy, right click on the Console window and select the "Clear console history" option from the context menu.

Golden rule: You cannot delete anything declared with var

If you want to hack your way through cleaning up the global scope you could identify what you've added by exploring the keys of the window object.

Object.keys(window)

Make a note of the keys you want to remove and then try to delete them:

['a', 'x'].map(prop => delete window[prop]);

Delete will try to remove the property from the window object and returns a boolean result, so you might end up with a response like

[true, false]

This indicates the first item was deleted but unfortunately the second was not.

In your case you could assign the x variable to null so it is marked for garbage collection but when will the window object go out of scope?

Dan Nagle
  • 4,384
  • 1
  • 16
  • 28
-1

There clear function just removes the text from the console.

There is no mechanism to remove all existing variables or clearing the cache from JS.

Open the Network tab and tick "Disable cache" then refresh the page to do that.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
-1

I was facing below issue with const variables declared within snippet when i tried to run the snippet multiple times without refreshing the page.

 SyntaxError: Identifier 'MY_CONST_VARIABLE' has already been declared

If you have created variable within console or snippet and want to clear just those variables but not the variables created by js file added to your html page using script tag, then you can just refresh the browser page and all variables created by you using "console and snippet" will get removed.

Swap
  • 319
  • 2
  • 6