jQuery API

.removeData()

.removeData( [name] ) Returns: jQuery

Description: Remove a previously-stored piece of data.

  • version added: 1.2.3.removeData( [name] )

    nameA string naming the piece of data to delete.

  • version added: 1.7.removeData( [list] )

    listAn array or space-separated string naming the pieces of data to delete.

The .removeData() method allows us to remove values that were previously set using .data(). When called with the name of a key, .removeData() deletes that particular value; when called with no arguments, all values are removed. Removing data from jQuery's internal .data() cache does not effect any HTML5 data- attributes in a document; use .removeAttr() to remove those.

When using .removeData("name"), jQuery will attempt to locate a data- attribute on the element if no property by that name is in the internal data cache. To avoid a re-query of the data- attribute, set the name to a value of either null or undefined (e.g. .data("name", undefined)) rather than using .removeData().

As of jQuery 1.7, when called with an array of keys or a string of space-separated keys, .removeData() deletes the value of each key in that array or string.

As of jQuery 1.4.3, calling .removeData() will cause the value of the property being removed to revert to the value of the data attribute of the same name in the DOM, rather than being set to undefined.

Example:

Set a data store for 2 names then remove one of them.

<!DOCTYPE html>
<html>
<head>
  <style>
  div { margin:2px; color:blue; }
  span { color:red; }
  </style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <div>value1 before creation: <span></span></div>
  <div>value1 after creation: <span></span></div>
  <div>value1 after removal: <span></span></div>

  <div>value2 after removal: <span></span></div>
<script>

    $("span:eq(0)").text("" + $("div").data("test1"));
    $("div").data("test1", "VALUE-1");
    $("div").data("test2", "VALUE-2");
    $("span:eq(1)").text("" + $("div").data("test1"));
    $("div").removeData("test1");
    $("span:eq(2)").text("" + $("div").data("test1"));
    $("span:eq(3)").text("" + $("div").data("test2"));

</script>

</body>
</html>

Demo:

Support and Contributions

Need help with .removeData() or have a question about it? Visit the jQuery Forum or the #jquery channel on irc.freenode.net.

Think you've discovered a jQuery bug related to .removeData()? Report it to the jQuery core team.

Found a problem with this documentation? Report it on the GitHub issue tracker

  • Scott Buchanan

    Note that removeData() will remove event handlers on matched elements when called without an argument.

  • dnagirl

    If you need to use .empty() and .removeData() on the same element, do .removeData() first. IE8 fails silently on .removeData() if .empty is run first.

    e.g.
    //Problem
    `$('.thing').empty().removeData(); //data is still populated in IE8`

    //No Problem
    `$('.thing').removeData().empty();//works as expected in both FF and IE8

  • dnagirl

    If you need to use .empty() and .removeData() on the same element, do .removeData() first. IE8 fails silently on .removeData() if .empty is run first.

    e.g.

    //Problem

    `$('.thing').empty().removeData(); //data is still populated in IE8`

    //No Problem

    `$('.thing').removeData().empty();//works as expected in both FF and IE8