Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP


State save clear #255

DataTables opened this Issue · 16 comments

6 participants


Might be useful to have an API method which will clear the save state - based on mars' comment here:


does anybody need this feature?


Its a feature request that pops up now and then, so yes I'm sure some would benefit from it, but I'm not yet sure if I'm going to include it in DataTables core. The way I'd like to see it implemented is in the new API with the following methods defined:

thanks indeed.


I'd like to see this feature also so we will not have to manually kill all the cookies when the user logs out from our applications. There are other use cases for it also, but I do not need to go into the details. It definitely would be useful though.


I'm writing a web page where the user can load page column order and visibility from db.
Now I have to reload the page
If datatable has such features I can apply configuration without page reload.


Has this been added in 1.10 .. I dont dont see any option to clear local storage in v1.10. If it has been added can someone post sample code on how to clear localstoreage.


No it hasn't - that is why the bug is still open. I will be looking at adding this soon.


If you are using the built in state saving functions of DataTables, then you can place the following somewhere in your code after the DataTables code:

    $.fn.dataTable.Api.registerPlural('tables().state.clear()', 'table().state.clear()', function(opts) {
        return this.iterator('table', function(ctx) {
            try {
                (ctx.iStateDuration === -1 ? sessionStorage : localStorage).removeItem(
            } catch(e) {}

    $.fn.dataTable.Api.register('state.clearAll()', function() {
        var clearStorage = function(key, storage) {
            if(key.match(/^DataTables_/) !== null) {
                try {
                } catch(e) {}

        try {
            $.each(window.localStorage, function(key) {
                clearStorage(key, window.localStorage);

            $.each(window.sessionStorage, function(key) {
                clearStorage(key, window.sessionStorage);
        } catch(e) {}

        return this;

This will add a couple of new methods to the API.

table().state.clear() - Clear any saved state information for a specific table.
tables().state.clear() - Clear any saved state information for all/multiple tables on page.
state.clearAll() - Clear all saved state information for all DataTables (including tables not rendered on the current page or on other pages in the same domain).


$(document).ready(function() {
    var dt = $('#DataTable1').DataTable();

    // Clear save state for #DataTable1
    dt.table().state.clear(); // You can also call draw() here if needed

    // Clear save state for #DataTable2

    // Clear save state for all tables on the page

    // Clear save state for all tables on the domain

Hi, thanks for this. Funnily enough I'm working on this at the moment. Writing the documentation for the new methods which are:

  • state() - get last saved state
  • - trigger a state save
  • state.loaded() - get state that was loaded when the table was initialised
  • state.clear() - clear the state

@DataTables Haha, perfect. I was in need of it so I figured I would just go ahead and put something together. Beat me to it ;)


Heh - bug fixes are like buses. Wait for months and then they all come at once...

Commit for the src repo

And this repo is currently syncing up and I'll rebuild the nightly shortly.

Let me know if you hit any problems with the new methods!

@DataTables DataTables closed this

I think you have forgotten to add those methods to JQuery API.
I mean, they works with .DataTable(), but not with .dataTable()


No - the legacy API will not have new features added to it. Indeed the legacy API is no longer documented on the site (although the old docs are still available at ).

The intention is that .dataTable() should just return a plain jQuery object while .DataTable() will return a DataTables specific API.


Maybe this doc should explain this difference better, when it says "The result from each is an instance of the DataTables API object", I thought both were the same. So dataTable is the deprecated legacy api.
Now I get it ... thanks for the clarification.
Great tool.


"The result from each is an instance of the DataTables API object"

That is an accurate statement. The documentation specifically lists:

  • $( selector ).DataTable();
  • $( selector ).dataTable().api();
  • new $.fn.dataTable.Api( selector );

Note the .api() on the second one.

It then also goes on to say:

It is important to note the difference between $( selector ).DataTable() and $( selector ).dataTable(). The former returns a DataTables API instance, while the latter returns a jQuery object.

I'm not sure what is unclear about that. How can it be improved?


:-). No worries. Thanks for keeping me on my toes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.