Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

219
Views
How to remove local storage in javascript

I am studying JavaScript and have been stuck on this for weeks! I need to remove these for items from local Storage. But using localStorage.removeItem('diary'); Will not work.

// Make a demo text item
    data =
        "Friday: We arrived to this wonderful guesthouse after a pleasant journey " +
        "and were made most welcome by the proprietor, Mike. Looking forward to " +
        "exploring the area tomorrow.";
    item = makeDiaryItem("text", data);

    // Make a key using a fixed timestamp
    key = "diary" + "1536771000001";

    // Store the item in local storage
    localStorage.setItem(key, item);

    // Make a demo text item
    data =
        "Saturday: After a super breakfast, we took advantage of one of the many " +
        "signed walks nearby. For some of the journey this followed the path of a " +
        "stream to a charming village.";
    item = makeDiaryItem("text", data);

    // Make a key using a fixed timestamp
    key = "diary" + "1536771000002";

    // Store the item in local storage
    localStorage.setItem(key, item);`

Can someone point me in the right direction? Not looking for a direct answer.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You need to specify correct keyname that you want to remove from the Local Storage.

In your code, you are setting the item in the Local Storage with the keyname "diary1536771000002". BUT, you are trying to remove the item with keyname "diary".

Try this instead:

localStorage.removeItem('diary1536771000002');
about 4 years ago · Juan Pablo Isaza Report

0

You should remove item with the correct key. In your case, the key would be the combination between diary and timestamp

It's possibly like this (because I'm seeing you're using that combination with key variable)

localStorage.removeItem(key);

If it does not work, you can inspect your browser to see the proper key of your local storage (right click on your page and select Inspect and after that you can look for Application tab)

enter image description here

about 4 years ago · Juan Pablo Isaza Report

0

If you want to remove all local storage items that begin with a prefix (e.g. "diary"), you could use a function like this:

TS Playground

function removeLocalStorageItems (matcher) {
  const keys = [];

  for (let i = 0; i < localStorage.length; i += 1) {
    const key = localStorage.key(i);
    if (key) keys.push(key);
  }

  const shouldRemove = typeof matcher === 'function' ? matcher : (str) => matcher.test(str);

  for (const key of keys) {
    if (shouldRemove(key)) localStorage.removeItem(key);
  }
}

function removeLocalStorageItemsByPrefix (prefix) {
  removeLocalStorageItems(key => key.startsWith(prefix));
}


// Use:

removeLocalStorageItemsByPrefix('diary');
// or
removeLocalStorageItems(/^diary/);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!